0

I have a table where I want to run a query that I get from a query.

enter image description here

What I want to do is return the name (checkbooks, clients, etc) and the results of the sql queries per each type. all within one query result so I can send that data out. I have no clue where to start.

TheDizzle
  • 1,534
  • 5
  • 33
  • 76

2 Answers2

0

You could try using your first query to open a cursor, then within the loop execute the result string as dynamic SQL. I dont know what your tables or columns are called, but assuming table is called Table and column containing those Selects is Result, you could try something like:

declare commands cursor for 

select result from table

declare @cmd varchar(max)

open commands
fetch next from commands into @cmd
while @@FETCH_STATUS=0
begin
  exec(@cmd)
  fetch next from commands into @cmd
end

close commands
deallocate commands

Inspired by this question: execute result of select statement

karlis ssl
  • 143
  • 4
0

This doesn't look like a good idea, unless there is no way for you to change this approach.

If you are using EF, after getting the row, you can execute the query in the query test field like this context.MyEntity.SqlQuery("SELECT * FROM dbo.MyEntity").ToList();

https://learn.microsoft.com/en-us/ef/ef6/querying/raw-sql

You can do the same with different ORM, the idea is simple get the string in the field and pass it to the execute query.

FYI: Is saving SQL statements in a table for executing later a bad idea? https://softwareengineering.stackexchange.com/questions/227922/is-saving-sql-statements-in-a-table-for-executing-later-a-bad-idea

Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301