SQL Server multiple Execution or using XML


  • I am having a requirement to store the where Condition in a column (ex:Site='MSDN' and Topics='SQL' etc) and need to execute it without using EXEC or anyother execute method.  i want to use this in Stored procedure.  If any other alternate method is available please share how we can bind or append and execute the same Or share you views or login that can be use for this (like store in XML datatype etc)

    I just give the example how i required.

    "Declare @Sites table (ID INT, Filter_Condition varchar(1000))

    select Filter_Condition from @Sites
    I am having a table called "Sites" in this table i am having a column called "Condition".  Where i store the condition or entire select query.  When i put select query in "Sites" table (Select Condition from Sites) it will fetch all records available in the table.  I want to execute the resultset again, like in 1st row i am having one select query, that needs to execute it again and so on.

    I want to execute the result which i am getting when i am executing the above query"  2 select queries need to exeucte

    25 มกราคม 2555 10:43


  • Hi Sundararaman R,

    It seems that you are trying to execute the string query which is as the value returened from a SELECT statement. You can execute as the dynamic SQL by EXEC or sp_executesql, which can also be included in a Stored Procedure. See: Execute Dynamic SQL commands in SQL Server.

    The question is more related to T-SQL, so i would like to move it to SQL Server T-SQL forum.

    Stephanie Lv

    TechNet Community Support

    27 มกราคม 2555 7:17
  • Hi Stephanie,

    Yes you are correct.  I want to execute the string query. but without using EXEC or any other commands i want to acheive this. is it possible to execute the query or is there any other way to execute it (through XML or any kind of other possible ways)?

    Is it possible to use XML ,Xquery or any other thing?
    • แก้ไขโดย Sundararaman R 27 มกราคม 2555 8:08
    27 มกราคม 2555 7:59
  • The only way to conditionally run different statements would be either using a CASE or an IF statement. But this would require to hardcode the WHERE clause in each and every statement making the whole concept more than silly....

    Is there any specific reason not to EXEC (or better sp_executesql)?

    24 กุมภาพันธ์ 2555 11:27
  • Hi,

    The reason is, if we are using the Exec or SP_ExecuteSQL.  Both will check the query vaildation in run time only, and its not compiled code also.  Each and evertime it will check the SQL statement and then only it will execute. 

    But already if we have the query SP, then its compiled code.  So based on the parameters the value only get changed that is the reason.

    My exact requirement is, I will store the where condition string or entire select query in a Column.  I need to fetch the column and need to execute it.

    For ex : i am having a table called "tblConditions" here i will store the where or select condition in one column like "Name='Robert' and Age = 20" like this or "Select * from tblconditions where Name='Robert' and Age = 20 "

    I want to take this condition from select query and need to execute the output of the select clause.

    • แก้ไขโดย Sundararaman R 29 กุมภาพันธ์ 2555 9:19
    29 กุมภาพันธ์ 2555 9:17