April 6, 2006 at 8:25 am
Hi,
I am trying to delete a few rows from a table variable, but I keep getting hit by 'Must declare the scalar variable "@tbl"' The table itself is working, I can insert data and I can display the table, just not delete from it. Any suggestions out there?
The bit of code I am using looks like this:
DECLARE @tbl TABLE (ID int, FirstName varchar(50))
...
DELETE FROM @tbl
WHERE @tbl.ID = (SELECT dbo.PersonList_Person.PersonID FROM dbo.PersonList_Person WHERE dbo.PersonList_Person.Include = 0)
Hope you can help me out here!
Best regards,
Kris
April 7, 2006 at 4:25 am
if you drop the table name prefix in the where so it looks like...
DELETE FROM @tbl
WHERE ID = (SELECT dbo.PersonList_Person.PersonID FROM dbo.PersonList_Person WHERE dbo.PersonList_Person.Include = 0)
or
DELETE @tbl FROM @tbl as t
WHERE t.ID = (SELECT dbo.PersonList_Person.PersonID FROM dbo.PersonList_Person WHERE dbo.PersonList_Person.Include = 0)
Thanks Jeet
April 7, 2006 at 6:22 am
Thanks Jeet,
Tried the first one and it works!
Best regards,
Kris
February 11, 2010 at 1:50 pm
Hi,
I have created a temp table (@TableName) with columns EmployeeId, EmployeeName and have inserted rows dynamically.
Now, I want to delete the those employees whose employeeIds are not in @EmpId
Below is my code for delete statement. I am getting "Invalid column name EmployeeId"
DECLARE @TableName varchar(300);
SET @TableName ='TEMPTABLE_' + CAST(@@SPID AS VARCHAR) + CONVERT(VARCHAR(255), NEWID());
SET @Query=' DELETE FROM '+ quotename(@TableName) +' WHERE '+ @EmpId +' NOT LIKE %'+cast(EmployeeId as varchar(3)) +'%';
execute(@Query)
Any help is appreciated
February 12, 2010 at 2:09 am
Hang on...
Why dynamically generated temp table names? Why dynamic SQL? Looks like you're making your life difficult for no reason.
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
February 12, 2010 at 2:21 am
Gail certainly has a point there.
But to answer your question: SQL Server thinks EmployeeID is a column from a table, but you don't tell it which table it shoud come from.
Either you have forgotten to prefix with @ (in case EmployeeID is actually a variable) or you should turn the statment into a complete SELECT statement e.g.
SELECT @Query = ' DELETE FROM '+ quotename(@TableName) +' WHERE '+ @EmpId +' NOT LIKE %'+cast(EmployeeId as varchar(3)) +'%'
FROM some_table;
which will result in a useless statement if the query returns more than 1 record...
February 12, 2010 at 2:24 am
You may find this article on dynamic SQL and when (not) to use it interesting.
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy