I have an integer column named "status" that I'd like to use against a
variable with a list. For example:
This works fine:
update table1 SET
column1 = @.var
where status IN (1,5)
This throws an error:
declare @.thislist as string
SET @.thislist = "(1,5)"
select top 10 id
from table1
where status in @.thislist
Error thrown is:
Server: Msg 170, Level 15, State 1, Line 6
Line 6: Incorrect syntax near '@.thislist'.
Any suggestions on how I can get the list in as a var? That list will
change. Any other approaches are welcome.
Thanks,
BrettTry,
declare @.sql nvarchar(4000)
declare @.thislist nvarchar(1000)
SET @.thislist = "(1,5)"
set @.sql = N'
select top 10 id
from table1
where status in' + @.thislist
exec sp_executesql @.sql
go
Arrays and Lists in SQL Server
http://www.sommarskog.se/arrays-in-sql.html
The Curse and Blessings of Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html
AMB
"Brett" wrote:
> I have an integer column named "status" that I'd like to use against a
> variable with a list. For example:
> This works fine:
> update table1 SET
> column1 = @.var
> where status IN (1,5)
>
> This throws an error:
> declare @.thislist as string
> SET @.thislist = "(1,5)"
> select top 10 id
> from table1
> where status in @.thislist
> Error thrown is:
> Server: Msg 170, Level 15, State 1, Line 6
> Line 6: Incorrect syntax near '@.thislist'.
> Any suggestions on how I can get the list in as a var? That list will
> change. Any other approaches are welcome.
> Thanks,
> Brett
>
>|||Why in the estimated execution plan this has no execution time?
Also, what is the N for?
Thanks,
Brett
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:D09B9A03-8659-4F37-9460-FE232BC1BA97@.microsoft.com...
> Try,
> declare @.sql nvarchar(4000)
> declare @.thislist nvarchar(1000)
> SET @.thislist = "(1,5)"
> set @.sql = N'
> select top 10 id
> from table1
> where status in' + @.thislist
> exec sp_executesql @.sql
> go
> Arrays and Lists in SQL Server
> http://www.sommarskog.se/arrays-in-sql.html
> The Curse and Blessings of Dynamic SQL
> http://www.sommarskog.se/dynamic_sql.html
>
> AMB
> "Brett" wrote:
>|||> Also, what is the N for?
http://www.aspfaq.com/2522sql
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment