SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Must declare the scalar variable "@Rank


Must declare the scalar variable "@Rank

Author
Message
Saravanakumar.Rathinam
Saravanakumar.Rathinam
SSC Veteran
SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)

Group: General Forum Members
Points: 293 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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)

Group: General Forum Members
Points: 226689 Visits: 46333
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, MVP, M.Sc (Comp Sci)
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


Saravanakumar.Rathinam
Saravanakumar.Rathinam
SSC Veteran
SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)

Group: General Forum Members
Points: 293 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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)

Group: General Forum Members
Points: 226689 Visits: 46333
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, MVP, M.Sc (Comp Sci)
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


steve smith-401573
steve smith-401573
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2471 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
Saravanakumar.Rathinam
Saravanakumar.Rathinam
SSC Veteran
SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)SSC Veteran (293 reputation)

Group: General Forum Members
Points: 293 Visits: 493
thnaks for your reply. i will check with my store procedure and reply you back Gail.

thanks & regards
Saravanakumar.R
GilaMonster
GilaMonster
SSC Guru
SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)SSC Guru (226K reputation)

Group: General Forum Members
Points: 226689 Visits: 46333
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, MVP, M.Sc (Comp Sci)
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search