Tupacmoche
Registered User.
- Local time
- , 22:57
- Joined
- Apr 28, 2008
- Messages
- 291
Hi SQL Masters,
I have a straight forward SQL code that, I decided to add a variable to the where clause. I have tried writing it many different ways but keep getting an error message. Before, making it into a variable it worked fine but now, I get the error message:
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@TableID".
(1 row(s) affected)
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@TableID".
(1 row(s) affected)
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@TableID".
(1 row(s) affected)
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@TableID".
(1 row(s) affected)
But the variable is declared. Here is the code:
USE Temp_Upload_Tables
DECLARE @TableCount tinyint
DECLARE @SQL varchar(2000)
DECLARE @TableName varchar(100)
DECLARE @TableID varchar(6)
DECLARE @TblAlias Varchar(1)
--------------------------------------------------
/* First create temp table and insert values */
/* This tbl will be used to loop through the */
/* tables that are updated. */
CREATE TABLE #TableList
(
TableName Varchar(100),
TableID Varchar(6),
TableA Varchar(1)
)
INSERT INTO #TableList(TableName, TableID, TableA)
VALUES ('tmpEpic_Name','EN','m');
INSERT INTO #TableList(TableName, TableID, TableA)
VALUES ('tmpEpic_history','EH','h');
INSERT INTO #TableList(TableName, TableID, TableA)
VALUES ('tmpEpic_screening_history','ESH','s');
INSERT INTO #TableList(TableName, TableID, TableA)
VALUES ('tmpEpic_request_history','ERH','r');
-----------------------------------------------------
SELECT @TableCount = COUNT(1) from #TableList
WHILE @TableCount > 0
BEGIN
Select Top 1 @TableName = TableName, @TableID = TableID From #TableList
SET @SQL = 'Update MRN_Processing
SET SourceIs = (Case When e.Match = ''Y'' then 1 else 0 end),
TargetIs = (Case When n.Match = ''Y'' then 1 else 0 end)
from MRN_Processing as m
Left join
(Select *, ''Y'' as Match
from ' + @TableName + '
) as e on e.MRN = m.SourcePatientMRN
Left join
( Select *, ''Y'' as Match
from ' + @TableName + '
) as n on n.MRN = m.TargetPatientMRN
Where tbl_id = + @TableID'
EXEC (@SQL)
DELETE #TableList WHERE TableName = @TableName
SELECT @TableCount = COUNT(1) from #TableList
END
Does anyone see what is wrong?
I have a straight forward SQL code that, I decided to add a variable to the where clause. I have tried writing it many different ways but keep getting an error message. Before, making it into a variable it worked fine but now, I get the error message:
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@TableID".
(1 row(s) affected)
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@TableID".
(1 row(s) affected)
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@TableID".
(1 row(s) affected)
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@TableID".
(1 row(s) affected)
But the variable is declared. Here is the code:
USE Temp_Upload_Tables
DECLARE @TableCount tinyint
DECLARE @SQL varchar(2000)
DECLARE @TableName varchar(100)
DECLARE @TableID varchar(6)
DECLARE @TblAlias Varchar(1)
--------------------------------------------------
/* First create temp table and insert values */
/* This tbl will be used to loop through the */
/* tables that are updated. */
CREATE TABLE #TableList
(
TableName Varchar(100),
TableID Varchar(6),
TableA Varchar(1)
)
INSERT INTO #TableList(TableName, TableID, TableA)
VALUES ('tmpEpic_Name','EN','m');
INSERT INTO #TableList(TableName, TableID, TableA)
VALUES ('tmpEpic_history','EH','h');
INSERT INTO #TableList(TableName, TableID, TableA)
VALUES ('tmpEpic_screening_history','ESH','s');
INSERT INTO #TableList(TableName, TableID, TableA)
VALUES ('tmpEpic_request_history','ERH','r');
-----------------------------------------------------
SELECT @TableCount = COUNT(1) from #TableList
WHILE @TableCount > 0
BEGIN
Select Top 1 @TableName = TableName, @TableID = TableID From #TableList
SET @SQL = 'Update MRN_Processing
SET SourceIs = (Case When e.Match = ''Y'' then 1 else 0 end),
TargetIs = (Case When n.Match = ''Y'' then 1 else 0 end)
from MRN_Processing as m
Left join
(Select *, ''Y'' as Match
from ' + @TableName + '
) as e on e.MRN = m.SourcePatientMRN
Left join
( Select *, ''Y'' as Match
from ' + @TableName + '
) as n on n.MRN = m.TargetPatientMRN
Where tbl_id = + @TableID'
EXEC (@SQL)
DELETE #TableList WHERE TableName = @TableName
SELECT @TableCount = COUNT(1) from #TableList
END
Does anyone see what is wrong?
