Results 1 to 3 of 3

Thread: dynamic queries

  1. #1
    Join Date
    Aug 2007
    Posts
    11

    Question Unanswered: 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

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    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:
    pootle flump
    ur codings are working excelent.

  3. #3
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •