Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Posts
    2

    Unanswered: How to execute a query from contained in a variable in a stored procedure

    Hi,

    How to execute a query conained in a variable ? I explain :

    CREATE PROCEDURE [dbo].[GetHeaderListExt2]
    @Banc varchar(50)
    AS
    declare @SqlQuery varchar(500)

    set @SqlQuery = "Select * from T_DataInfos"

    select @SqlQuery

    The " select @SqlQuery " return me the content of the variable; in fact it is not false but how to execute the content of the variable ?

    I have created an SP that generate a query that I want to execute and return the result of that query, an the query is stored in a variable...

    Thanks a lot...

    RayBan

  2. #2
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809
    try:

    EXECUTE(@SqlQuery)
    Paul Young
    (Knowledge is power! Get some!)

  3. #3
    Join Date
    Jan 2003
    Location
    Leamington Spa, UK
    Posts
    17

    Re: How to execute a query from contained in a variable in a stored procedure

    Take a look at EXECUTE (or EXEC) or sp_executesql in Books Online (BOL).

    Usage:

    DECLARE @var AS VARCHAR(500)
    SET @var='SELECT * FROM SomeTable'
    EXECUTE (@var)

    or

    DECLARE @var AS NVARCHAR(500)
    SET @var='SELECT * FROM SomeTable'
    EXECUTE sp_executesql @var


    macka.

    ps. If you actually need to 'use' the results of the dynamic query (in a subsequent query), you may need to investigate using temporary tables.

  4. #4
    Join Date
    Jan 2003
    Posts
    2
    Thanks a lot guys...

    The EXECUTE(@MyQuery)

    work so fine....

Posting Permissions

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