If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > dynamic queries

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-27-09, 06:28
shailesh80 shailesh80 is offline
Registered User
 
Join Date: Aug 2007
Posts: 11
Question dynamic queries

Hi
I would like to create a stored procedure and construct dynamic sql statement and execute it.
For example: select e.empid, e.empname, d.departmentname, e.departmentid
FROM employee e, department d
I want "where" clause to be dynamic in nature.
I tried with the follwing ways
Declare @SQL nvarchar(Max)
select @SQL = N'select e.empid, e.empname, d.departmentname, e.departmentid
FROM employee e, department e
WHERE e.departmentid=d.departmentid'

-- Run the query
exec @SQL
But then it gives error, incorrect syntax
How can i achieve this?

Thanks in advance
__________________

--
Shailesh Patil
Reply With Quote
  #2 (permalink)  
Old 04-27-09, 06:45
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,907
This will cover everything you need to know.
The curse and blessings of dynamic SQL
Hopefully, it will make you reconsider your approach too.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #3 (permalink)  
Old 04-27-09, 07:51
shailesh80 shailesh80 is offline
Registered User
 
Join Date: Aug 2007
Posts: 11
Thanks for your reply.
I will go through the article and incase of any doubts i will get back.
thanks once again
__________________

--
Shailesh Patil
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On