Saturday, February 25, 2012

Problem with variable in T-SQL

Hi,

I want to find out the project names, for the PROJ table and I know
the Project IDs as a string (comma seperated).

I tried this:

--------
DECLARE @.proj_ids varchar(500)

SET @.proj_ids='(100,200)'

SELECT PROJ_NAME FROM PROJ WHERE PROJ_ID IN + @.proj_ids
--------

But this throws the error:
"Incorrect syntax near '+'"

in Query Analyzer.

Could someone please point, what I am doing wrong?
I know, I must be doing something very silly!Hi

Check out the excellent article at:
http://www.algonet.se/~sommar/dynamic_sql.html

John

"Surajit Laha" <s_laha@.rediffmail.com> wrote in message
news:ba8ee108.0310140529.3af4dd96@.posting.google.c om...
> Hi,
> I want to find out the project names, for the PROJ table and I know
> the Project IDs as a string (comma seperated).
> I tried this:
> --------
> DECLARE @.proj_ids varchar(500)
> SET @.proj_ids='(100,200)'
> SELECT PROJ_NAME FROM PROJ WHERE PROJ_ID IN + @.proj_ids
> --------
> But this throws the error:
> "Incorrect syntax near '+'"
> in Query Analyzer.
> Could someone please point, what I am doing wrong?
> I know, I must be doing something very silly!|||[posted and mailed, please reply in news]

Surajit Laha (s_laha@.rediffmail.com) writes:
> I want to find out the project names, for the PROJ table and I know
> the Project IDs as a string (comma seperated).
> I tried this:
> --------
> DECLARE @.proj_ids varchar(500)
> SET @.proj_ids='(100,200)'
> SELECT PROJ_NAME FROM PROJ WHERE PROJ_ID IN + @.proj_ids
> --------
> But this throws the error:
> "Incorrect syntax near '+'"
> in Query Analyzer.
> Could someone please point, what I am doing wrong?
> I know, I must be doing something very silly!

Yes, you are inventing your own syntax. SQL Server is not clever enough
to understand that.

Although, John recommended one article on my web site, I like to
recommend another. Look at
http://www.algonet.se/~sommar/array...ist-of-integers
to see a function that will help you out.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks a lot to you people!!!

The article at the URL pointed, was fabulous!
I really liked it and solved a lot of confusions of mine...

Thanks guys.

-surajit

No comments:

Post a Comment