Thread: TSQL = query

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2

    TSQL = query


    I think this is a much easier question

    and i have seen this done by other programmers, but i have forgotten how to do it.

    Can someone teach me how to create a stored procedure, with parameters which would look like this.

    CREATE PROCEDURE dbo.TestProc
    (
    @selectstatement varchar(50) = NULL,
    @whereclause varchar(50) = NULL,
    @orderby varchar(50) = NULL
    )

    AS

    tsql = '@selectstatement + @whereclause + @orderby'

    is this even possible?

    thanks for any ideas
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    103
    Rep Power
    9
    Yes you can do something like

    Code:
    CREATE PROCEDURE dbo.TestProc
    (
    @selectstatement varchar(50) = NULL,
    @whereclause varchar(50) = NULL,
    @orderby varchar(50) = NULL
    )
    
    AS
    
    declare @strSQL as varchar(8000)
    set @strSQL = @selectstatement + ' ' + @whereclause + ' ' + @orderby
    exec(@strSQL)
    I'm not sure it is very good idea, but you can
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    could you please give me some more code... how could i pass the select statement to the said parameters?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    103
    Rep Power
    9
    you can call your store procedure like

    Code:
    exec TestProc @selectstatement =' select * from yourtable',
    @whereclause = 'where id= 5 ',
    @orderby ='order by id '
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    31
    Rep Power
    2
    thanks men!

IMN logo majestic logo threadwatch logo seochat tools logo