Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Must declare the scalar variable "@Rank Expand / Collapse
Author
Message
Posted Thursday, February 7, 2008 5:07 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 11, 2008 1:05 AM
Points: 49, Visits: 493
while running the query in sp's I got the error message of

Msg 137, Level 15, State 1, Line 1
Must declare the scalar variable "@Rank".


SP is

SET @SQL = 'SELECT top 1 @Rank = K.RANK FROM REG_TEMP1 AS C INNER JOIN '
SET @SQL=@SQL + ' CONTAINSTABLE(REG_TEMP1,ST_Address_Name, ''ISABOUT("'
SET @SQL=@SQL +@db
SET @SQL=@SQL + '", '+@CC
SET @SQL=@SQL + ' WEIGHT(0.5),'
SET @SQL=@SQL +@db
SET @SQL=@SQL + ' WEIGHT(0.9))'',1) AS K on C.ID = K.[KEY] '

if i run the query without sp and the parameter is fixed it return values like
SELECT top 1 K.RANK, C.ST_Address_Name,C.st_address_Line_1,C.st_address_line_2,
C.ST_address_city,C.St_address_state,C.ST_address_zipcode,C.ST_address_country
FROM REG_TEMP1 AS C INNER JOIN CONTAINSTABLE(REG_TEMP1,ST_Address_Name, 'ISABOUT("CHESAPEAKE", HOSPITAL WEIGHT(0.5),CHESAPEAKE WEIGHT(0.9))',1) AS K on C.ID = K.[KEY]

Can you help me, how to resolve these query using sp's

thanks & regards
Saravanakumar.R
Post #452655
Posted Thursday, February 7, 2008 5:24 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:14 PM
Points: 39,977, Visits: 36,340
Variable declared outside dynamic SQL aren't accessible within it, and vis versa

Your first line of dynamic SQL reads 'SELECT top 1 @Rank = K.RANK...'
Within the dynamic SQL, the variable @Rank has not been declared.

If you're trying to get values out of dynamic sQL, then look up sp_executesql in books online and note the sections on declaring and passing parameters.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #452664
Posted Thursday, February 7, 2008 5:37 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 11, 2008 1:05 AM
Points: 49, Visits: 493
i declared initially

Declare @Rank as varchar(2000)
set @Rank=null

then
Sp'Query
but passing the values is from table.

thanks & regards
Post #452668
Posted Thursday, February 7, 2008 5:49 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:14 PM
Points: 39,977, Visits: 36,340
It's declared outside of the dynamic SQL string. If you want the variable accessible inside the dynamic SQL, you either have to declare it within the dynamic SQL string, or use sp_executesql and declare it as a parameter

This will throw an error - variable not declared

DECLARE @sql varchar(max), @SomeVar int
SET @SomeVar = 0
SET @sql = 'SELECT @SomeVar'
EXEC (@sql) -- error @somevar not declared

This will work fine.

DECLARE @sql varchar(max)
SET @SQL = 'DECLARE @SomeVar int'
SET @SQL = @SQL + ' SET @SomeVar = 0 '
SET @SQL = @SQL + ' SELECT @SomeVar'

EXEC (@sql)

This will also work fine

DECLARE @sql nvarchar(max), @SomeVar int
SET @SomeVar = 0
SET @sql = 'SELECT @SomeOtherVar'

exec sp_executesql @sql, N'@SomeOtherVar int', @SomeOtherVar = @SomeVar



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #452674
Posted Thursday, February 7, 2008 9:34 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Today @ 2:26 PM
Points: 691, Visits: 744
Gail - a general question and a hair split. First the hair split - do you need a trailing space on the first line of your "SET @SQL = 'text'" so it reads ...'text '?

General question - do you need to delimit your SQL statements in some way so SQL Server knows where one statement ends and another begins, or is that handled automatically by using separate SET statements? I haven't had the chance to use this in so long, I've forgotten (and being too lazy to look it up).

Steve
Post #452814
Posted Thursday, February 7, 2008 10:31 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 11, 2008 1:05 AM
Points: 49, Visits: 493
thnaks for your reply. i will check with my store procedure and reply you back Gail.

thanks & regards
Saravanakumar.R

Post #453067
Posted Thursday, February 7, 2008 11:31 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:14 PM
Points: 39,977, Visits: 36,340
1) No, however if you're concatenating pieces of string together, you'll need a space somewhere.

2) No. SQL doesn't care about whitespace. Lay things out that they are readable by you and anyone reading the code in the future.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #453077
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse