SqlServer question...

ajf3

Platinum Member
Oct 10, 2000
2,566
0
76
Hi all,

Had a sql question for you SQL folks out there... using sqlserver:

DECLARE @MyVar varchar
SET @MyVar = "('one', 'two')"

I'm able to do this:

SELECT * FROM Table WHERE field = @MyVar

...but not this:

SELECT * FROM Table WHERE field IN @MyVar

Is there a way to do the 2nd SELECT without building up a string and running it via EXEC(), or is that the way it has to be done?

Thanks!
 

shutterFly

Member
Nov 5, 2003
57
0
0
Try this:

DECLARE @MyVar varchar
SET @MyVar = ',one,two,'

SELECT * FROM Table WHERE CHARINDEX(',' + field + ',', @MyVar) > 0
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Hmm, I only know a little transact-sql but I could take a stab at it.

In your second example you're trying to do an IN on something that is not a tuple of any sort. What I think you're assuming is that the contents of @MyVar will get inserted into the sql statement as a string, which they won't. transact-sql is smarter than that.

Some options would be to actually make @MyVar a complex data type like some sort of result set, or to manually generate your sql string and then execute it like so:

@sql_string = 'select * from table where field in ' + @MyVar
exec_sql(@sql_string)

Or you could do what I think shutterFly is showing and parse your potential values out of @MyVar
 

ajf3

Platinum Member
Oct 10, 2000
2,566
0
76
Thx guys -

Right, I knew I could go about it with an exec, but wanted to see if there was another way to make sqlserver recognize @MyVar as part of the statement... I tried the charindex example - it didn't error, but didn't return any rows either...
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You should have a look through the manual and see if there are any complex data types that might work for this.
 

ajf3

Platinum Member
Oct 10, 2000
2,566
0
76
Thing is... I don't think I'd be able to pass that into a sp, would I?
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Are the single quotes in @MyVar properly escaped? What happens if you run "print @MyVar"?
 

ajf3

Platinum Member
Oct 10, 2000
2,566
0
76
Actually, a Print @MyVar just returns the 1st character of the string -ie, (
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
This is not possible in SQL server or probably any other RDBMS so easily. I am positive there is an article somewhere explaining a more complicated solution to this, but I couldn't find it easily with a google.
 

shutterFly

Member
Nov 5, 2003
57
0
0
Hmm... from these replies, I can see what might be the problem.

First, declare @MyVar as a VARCHAR(200) or something. Looks like you're only declaring it as a VARCHAR with no length. This will default it to a length of 1 as indicated by your PRINT attempt. Sorry, I should have been more clear about that.

Second, what was @MyVar when you tried CHARINDEX? Make sure you drop the parentheses and don't use double quotes. Also, make sure there are no spaces when delimiting the values.

Should look like: ',one,two,three,'

I know what you mean by not wanting to use an EXEC command on a string. It sort of defeats the purpose of having a pre-compiled stored procedure.

Good Luck!
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: ajf3
Actually, a Print @MyVar just returns the 1st character of the string -ie, (

Sounds like your single quotes are not escaped.

SET @MyVar = '(''one'',''two'')'
 

KentState

Diamond Member
Oct 19, 2001
8,397
393
126
Why don't you just pass a recordset into the stored procedure and use that to join to the other table?
 

ajf3

Platinum Member
Oct 10, 2000
2,566
0
76
Hey - it works

Changing varchar to varchar(200) allowed the charindex example to return the expected results - thanks!

 

ajf3

Platinum Member
Oct 10, 2000
2,566
0
76
Why don't you just pass a recordset into the stored procedure and use that to join to the other table?

Didn't realize that was possible... I'll have to look into it.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |