June 18, 2012 at 4:33 am
Hi,
I have this stored procedure:
ALTER PROCEDURE USERSGSC (@DB as varchar(50), @RESULTADO INT OUTPUT)
AS
BEGIN
DECLARE @STRSQL AS VARCHAR(5000),
@PARMDEFINITION AS NVARCHAR (4000),
@VALOR INT
SET @STRSQL = 'select @val = SELECT ISNULL(CODIGO,0) FROM '+@DB+'.DBO.UTILIZADORES WHERE NOME = ''sgsc'''
SET @PARMDEFINITION = '@Val INT OUTPUT'
EXECUTE sp_executesql @STRSQL , @PARMDEFINITION, @val = @VALOR OUTPUT
IF @VALOR = 0
BEGIN
SET @STRSQL ='INSERT INTO '+@DB+'dbo.UTILIZADORES values (''SGSC'',
''Utilizador criado para ser usado pelo SGSC''
,3,''1Yzw12ptag341*'',''administrador'',GETDATE()))'
EXEC (@STRSQL)
SET @STRSQL = 'select @val = SELECT ISNULL(CODIGO,0) FROM '+@DB+'.DBO.UTILIZADORES WHERE NOME = ''sgsc'''
SET @PARMDEFINITION = '@Val INT OUTPUT'
EXECUTE sp_executesql @STRSQL , @PARMDEFINITION, @val = @VALOR OUTPUT
RETURN @VALOR
END
ELSE
RETURN @VALOR
END
When I execute it like this:
EXEC USERSGSC 'SGCTCENTRAL',0
I get the following error:
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.
Can someone help?
Thanks
June 18, 2012 at 4:52 am
June 18, 2012 at 5:02 am
okbangas (6/18/2012)
Btw, I do hope you did not actually post an adminstrator password?
Not to mention storing passwords in plain text...
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 18, 2012 at 5:51 am
okbangas (6/18/2012)
... Btw, I do hope you did not actually post an adminstrator password?
It does look like the real one to me...
So, need an IP address and off you go!
:w00t:
June 19, 2012 at 2:48 am
Ok. When I change the type to Nvarchar it worked fine.
Now I have other problem with the same procedure.
Code:
ALTER PROCEDURE sp_SGCT_USERSGSC (@DB as varchar(50))
AS
BEGIN
DECLARE @STRSQL AS NVARCHAR(4000),
@PARMDEFINITION AS NVARCHAR (4000),
@VALOR INT
SET @STRSQL = 'select @val = isnull(CODIGO,0) FROM '+@DB+'.DBO.UTILIZADORES WHERE NOME = ''sgsc'''
SET @PARMDEFINITION = '@Val INT OUTPUT'
EXECUTE sp_executesql @STRSQL , @PARMDEFINITION, @val = @VALOR OUTPUT
IF @VALOR is null
BEGIN
SET @STRSQL ='INSERT INTO '+@DB+'dbo.UTILIZADORES values (''SGSC'',
''Utilizador criado para ser usado pelo SGSC''
,3,''1Yzw12ptag341*'',''administrador'',GETDATE()))'
EXEC (@STRSQL)
SET @STRSQL = 'select @val = CODIGO FROM '+@DB+'.DBO.UTILIZADORES WHERE NOME = ''sgsc'''
SET @PARMDEFINITION = '@Val INT OUTPUT'
EXECUTE sp_executesql @STRSQL , @PARMDEFINITION, @val = @VALOR OUTPUT
RETURN @VALOR
END
ELSE
RETURN @VALOR
END
When I execute it, I receive the following message:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ')'.
The 'sp_SGCT_USERSGSC' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.
The procedure should send a value of 0 instead of null given the code isnull(CODIGO,0) but it sends the value 0.
Can you help?
Thanks
June 19, 2012 at 2:57 am
This chunk has nothing preventing that return parameter being null
IF @VALOR is null
BEGIN
SET @STRSQL ='INSERT INTO '+@DB+'dbo.UTILIZADORES values (''SGSC'',
''Utilizador criado para ser usado pelo SGSC''
,3,''1Yzw12ptag341*'',''administrador'',GETDATE()))'
EXEC (@STRSQL)
SET @STRSQL = 'select @val = CODIGO FROM '+@DB+'.DBO.UTILIZADORES WHERE NOME = ''sgsc'''
SET @PARMDEFINITION = '@Val INT OUTPUT'
EXECUTE sp_executesql @STRSQL , @PARMDEFINITION, @val = @VALOR OUTPUT
RETURN @VALOR
END
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 19, 2012 at 3:04 am
June 19, 2012 at 4:02 am
If the first select returns no rows then @Valor will be null at the IF statement. There is nothing in the select inside the IF that prevents nulls.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 19, 2012 at 4:05 am
How can I do it?
Can you suggest how to do it?
Thanks in advance
June 19, 2012 at 4:59 am
How to do what?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 19, 2012 at 5:53 am
How to make this:
SET @STRSQL = 'select @val = isnull(CODIGO,0) FROM '+@DB+'.DBO.UTILIZADORES WHERE NOME = ''sgsc'''
SET @PARMDEFINITION = '@Val INT OUTPUT'
EXECUTE sp_executesql @STRSQL , @PARMDEFINITION, @val = @VALOR OUTPUT
Pass the value 0 to the variable @valor when there are no NOME equals to SGSC
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply