Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > Database

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 10-09-2009, 10:44 AM
priyamtheone priyamtheone is offline
Registered User
 
Join Date: Mar 2009
Posts: 11
Question MSSQL Server 2005- Problem creating stored procedure.

I'm trying to create a stored procedure in MSSQL Server 2005 that'll perform the following jobs:
1) Create a login.
2) Create an user in TestDB database for the login created in step 1.
3) Assign the role 'db_generaluser' to the user created in step 2.

The login name and password for the login to be created will be supplied from externally through input parameters. If this procedure executes successfully it returns 0 else 1 to the caller through an output parameter.

When I try to compile it, it's prompting the following error messages:
Msg 102, Level 15, State 1, Procedure sp_CreateLogin, Line 23
Incorrect syntax near '@loginname'.

Msg 319, Level 15, State 1, Procedure sp_CreateLogin, Line 23
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

Msg 319, Level 15, State 1, Procedure sp_CreateLogin, Line 27
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

What's my mistake? Plz help. My code follows:

Code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ==========================================================================================================
-- Author:	Priyamtheone
-- Create date: 2009-Oct-08
-- Description:	This procedure is used to create a login, create an user in TestDB database associated to
--		that login and assign the role 'db_generaluser' to that user. The login name and password
--		will be supplied from externally through input parameters. If this procedure executes
--		successfully it returns 0 else 1 to the caller through an output parameter.
-- ==========================================================================================================
CREATE PROCEDURE [dbo].[sp_CreateLogin] 
	-- Add the parameters for the stored procedure here.
	@loginname sysname,
	@passwd	sysname,
	@intRetVal int = 0 output	--Output parameter.
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
	SET NOCOUNT ON;
	SET @intRetVal = 0;

	BEGIN TRY
		CREATE LOGIN @loginname WITH PASSWORD=@passwd, DEFAULT_DATABASE=[TestDB], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF

		CREATE USER @loginname FOR LOGIN @loginname WITH DEFAULT_SCHEMA=[dbo]

		EXEC sp_addrolemember N'db_generaluser', @loginname
	END TRY
	BEGIN CATCH
		SET @intRetVal = 1;	--Something went wrong. So set the return value to 1.
	END CATCH
END
GO
Reply With Quote
  #2  
Old 10-14-2009, 06:08 PM
glafmitzpery glafmitzpery is offline
Registered User
 
Join Date: Oct 2009
Posts: 5
Check here:

http://msdn.microsoft.com/en-us/libr...1(SQL.90).aspx

It says:

"login_name appears to be a literal value and will not accept variables. As a workaround try EXEC('CREATE LOGIN [' + quotename(@Username) + '] FROM WINDOWS') as an example."
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
stored procedure problem jabbarsb Database 3 08-02-2006 09:50 AM
Re: Temp.Table results from Stored Procedure into rs- ANSWER gavin VB Classic 4 04-22-2001 11:35 PM
A Stored Procedure Bogging down the Server Merrill Nelson authorevents.vieira 2 01-25-2001 01:00 AM
SQL Server 6.5 Speed Issue with Stored Procedure. Andrew Ross Database 3 11-17-2000 02:37 PM
Remove exchange from a site andriano Enterprise 1 10-02-2000 04:54 PM


All times are GMT -4. The time now is 02:31 AM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.