By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,696 Members | 2,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,696 IT Pros & Developers. It's quick & easy.

Problem in creating stored procedure.

P: 79
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:

Expand|Select|Wrap|Line Numbers
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. -- ==========================================================================================================
  6. -- Author:    Priyamtheone
  7. -- Create date: 2009-Oct-08
  8. -- Description:    This procedure is used to create a login, create an user in TestDB database associated to
  9. --        that login and assign the role 'db_generaluser' to that user. The login name and password
  10. --        will be supplied from externally through input parameters. If this procedure executes
  11. --        successfully it returns 0 else 1 to the caller through an output parameter.
  12. -- ==========================================================================================================
  13. CREATE PROCEDURE [dbo].[sp_CreateLogin] 
  14.     -- Add the parameters for the stored procedure here.
  15.     @loginname sysname,
  16.     @passwd    sysname,
  17.     @intRetVal int = 0 output    --Output parameter.
  18. AS
  19. BEGIN
  20.     -- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
  21.     SET NOCOUNT ON;
  22.     SET @intRetVal = 0;
  23.  
  24.     BEGIN TRY
  25.         CREATE LOGIN @loginname WITH PASSWORD=@passwd, DEFAULT_DATABASE=[TestDB], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
  26.  
  27.         CREATE USER @loginname FOR LOGIN @loginname WITH DEFAULT_SCHEMA=[dbo]
  28.  
  29.         EXEC sp_addrolemember N'db_generaluser', @loginname
  30.     END TRY
  31.     BEGIN CATCH
  32.         SET @intRetVal = 1;    --Something went wrong. So set the return value to 1.
  33.     END CATCH
  34. END
  35. GO
Oct 9 '09 #1
Share this Question
Share on Google+
2 Replies


ck9663
Expert 2.5K+
P: 2,878
Try removing the default_schema parameter. If you left it blank, it will default to 'dbo' anyway.

Good luck!!!


--- CK
Oct 14 '09 #2

nbiswas
100+
P: 149
Try something like
Expand|Select|Wrap|Line Numbers
  1. set @str = '
  2.         CREATE LOGIN ' + @loginname  + 'WITH PASSWORD=' + @passwd +', DEFAULT_DATABASE=[TestDB], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
  3.  
  4.         CREATE USER' +  @loginname + 'FOR LOGIN' +  @loginname + ' WITH DEFAULT_SCHEMA=[dbo]'
Hope it will work.. didnot test though
Nov 9 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.