Re: Dynamic SQL question
From: Roji. P. Thomas (lazydragon_at_nowhere.com)
Date: 07/21/04
- Next message: Roji. P. Thomas: "Re: Newbie here, how does subquery work in sql server?"
- Previous message: Roji. P. Thomas: "Re: CrossTab Queries"
- In reply to: John Dude: "Dynamic SQL question"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 21 Jul 2004 12:19:50 +0530
If you are going the Dynamic SQL way, make sure that you read
http://www.sommarskog.se/dynamic_sql.html
-- Roji. P. Thomas Net Asset Management https://www.netassetmanagement.com "John Dude" <jhd8989@hotmail.com> wrote in message news:uIZ5bepbEHA.3476@tk2msftngp13.phx.gbl... > Hi! > > I need to build a where clause for my SQL query. Please see the code > below: > > create table #Accounts > (ID int, > AccountName varchar(100)) > > insert into #Accounts values (1, '12345') > insert into #Accounts values (2, '99999') > > declare @SQL varchar(100) > declare @where varchar(20) > declare @ID int > declare @AccountName varchar(10) > > set @ID = 2 > set @AccountName = '12345' > > set @where = case when @ID is not null then 'ID = ' + > convert(varchar(20), @ID) end > case when @AccountName is not null then 'AccountName = ' + > @AccountName > > set @SQL = 'select ID, AccountName from #Accounts where ' + @where > --print @SQL > exec (@SQL) > > How do I build WHERE clause in such a way that if @ID and @AccountName > (both variables) are not nulls, the @WHERE clause would construct this > string: > ID = 2 AND > AccountName = '12345' > > If one is Null (lets say AccountName is null) then it would construct: > ID = 2 > > If both are Nulls, the @where clause would be just blank i.e. > select ID, AccountName from #Accounts > > How do I do this? > > THANX for your help! > > > > > *** Sent via Developersdex http://www.developersdex.com *** > Don't just participate in USENET...get rewarded for it!
- Next message: Roji. P. Thomas: "Re: Newbie here, how does subquery work in sql server?"
- Previous message: Roji. P. Thomas: "Re: CrossTab Queries"
- In reply to: John Dude: "Dynamic SQL question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|