|
|
|
SSC 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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:18 PM
Points: 37,744,
Visits: 30,025
|
|
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
|
|
|
|
|
SSC 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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:18 PM
Points: 37,744,
Visits: 30,025
|
|
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
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Friday, September 21, 2012 1:52 PM
Points: 696,
Visits: 743
|
|
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
|
|
|
|
|
SSC 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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:18 PM
Points: 37,744,
Visits: 30,025
|
|
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
|
|
|
|