SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


String Execution in SQL


String Execution in SQL

Author
Message
dsaran
dsaran
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 42
select * from t_system

O/p is
---
AS400
CPS
HBS

But the below code is not work

declare @i varchar(100)
set @i = 'as400,cps'
select * from t_system where systemcode in (@i)

anybody help


I need to implement this one to production db.
Actual problem is, when the client pass 'as400' server will respond only as400 and when the client pass 'CPS' server will respond only 'CPS'


But the client pass the argument 'ALL; server need to return all the above. How to implement anybody plz help!!!!!!!!!!!!!!!!!!!!!!!!!!!
Mahesh Bote
Mahesh Bote
SSCertifiable
SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)

Group: General Forum Members
Points: 5553 Visits: 1303

Try this...

declare @i varchar(100)
set @i = '''as400'',''cps'''
select * from t_system where systemcode in (@i)

Mahesh


MH-09-AM-8694
GSquared
GSquared
SSC Guru
SSC Guru (140K reputation)SSC Guru (140K reputation)SSC Guru (140K reputation)SSC Guru (140K reputation)SSC Guru (140K reputation)SSC Guru (140K reputation)SSC Guru (140K reputation)SSC Guru (140K reputation)

Group: General Forum Members
Points: 140505 Visits: 9731
Mahesh Bote (6/26/2008)

Try this...

declare @i varchar(100)
set @i = '''as400'',''cps'''
select * from t_system where systemcode in (@i)

Mahesh


That won't work. "In" doesn't work that way with variables.

There are a couple of ways you can do this. One is create a table of the system codes and query that as a sub-query or a join. Another is provide a comma-delimited list of system codes, and then query it with a string parser (there are some on the scripts page of this site).

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (512K reputation)SSC Guru (512K reputation)SSC Guru (512K reputation)SSC Guru (512K reputation)SSC Guru (512K reputation)SSC Guru (512K reputation)SSC Guru (512K reputation)SSC Guru (512K reputation)

Group: General Forum Members
Points: 512549 Visits: 44322
To wit...

http://www.sqlservercentral.com/articles/T-SQL/63003/

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search