Sql Execute Query String

21.05.2020by

Execute the create table inside of your dynamic SQL (ugly): DECLARE @query NVARCHAR(MAX); SET @query = 'DECLARE @specsAndModel TABLE (specName VARCHAR(50),specVal VARCHAR(50)) INSERT INTO @specsAndModel VALUES('modelNumber', 'F00-B4R') SELECT specName, specVal FROM @specsAndModel' exec(@query). Because the actual text of the Transact-SQL statement in the spexecutesql string does not change between executions, the query optimizer will probably match the Transact-SQL statement in the second execution with the execution plan generated for the first execution. Therefore, SQL Server does not have to compile the second statement.

Active1 month ago

I have to convert a MSSQL stored proc that passes a varchar that is a query:

This isn't working. I'm pretty sure that EXEC and EXECUTE aren't MySQL commands, but CALL doesn't work either.

Does anyone know if it's even possible to have something like JavaScript's eval function for MySQL?

Qantas 94 Heavy
12.6k15 gold badges57 silver badges75 bronze badges
aaronaaarona
15.8k38 gold badges108 silver badges157 bronze badges

4 Answers

EXECUTE is a valid command in MySQL. MySQL reference manual

Brandon BodnarBrandon Bodnar
7,8001 gold badge31 silver badges41 bronze badges
TengizTengiz

This is the SQL equivalent of eval(my_string);:

Basically I combined the existing answers, neither tells you how to do eval exactly.

If you want to add parameters, you can use this:

And to answer the original question exactly:

Note that the PREPARE .. FROM statement wants a session variable (prefixed with @). If you try to pass a normal variable, it will throw its hands up in the air and it just won't care.

kenorb
82.3k35 gold badges459 silver badges469 bronze badges
LucLuc
1,5762 gold badges24 silver badges30 bronze badges

The EXECUTE MySQL command can only be used for one prepared statement.

If case you want to execute multiple queries from the string, consider saving them into file and source it, e.g.

kenorbkenorb
82.3k35 gold badges459 silver badges469 bronze badges

Not the answer you're looking for? Browse other questions tagged mysqlevalevaluationexecexecute or ask your own question.

Active2 years, 6 months ago

I have an awesome query that populates my virtual table with a list of scripts that create non clustered indices for all foreign keys.

However this list is great but, I want to take it another step further and run a dynamic query to execute each script.

I don't really have that much exp with dynamic query's as most time I simply replicate functionality in my c# software instead.

So I have.

now this is not going to work as im returning mulitple rows.

What I need to do is to simply get each row and execute one after the other.

For the life of me my brain has had a melt down and I just cannot see it.

I know this is a simple fix, what am I missing?

UPDATE ::

Just to add here's what my script generates in each row of my table, table has just 1 column.

This code is what is in my rows.

As a treat I thought I would post this script that generates the scripts.

Atlas copco ga 18 vsd ff manual. Schematics.1202181SF2Atlas Copco SF2 Stationary Air Compressor Spare parts catalog. Schematics.1202182SF4Atlas Copco SF4 Stationary Air Compressor Spare parts catalog. Operation and maintenance manual. Operation and maintenance manual.

OK, I followed JKN solution but the generated code when checked does not like IF

the error is 'Incorrect syntax near the keyword 'IF'.' Again I am not sure what syntax is needed for dynamic sql, is there some syntax missing? Can anyone spot this?

peterh
6,93315 gold badges56 silver badges75 bronze badges
lemunk

Mysql Execute String

lemunk

Sql Execute A Query String

1,3898 gold badges48 silver badges81 bronze badges

1 Answer

You can use this syntax to build a string from a list of rows. Please be advised it's not officially supported and may cause issues if you start using things like ORDER BY:

Andriy M
66k14 gold badges81 silver badges134 bronze badges

C# Execute Sql Query String

JNK

Sql Server Execute Statement String

JNK
52.7k13 gold badges105 silver badges126 bronze badges

Sql Execute Query String In Excel

Not the answer you're looking for? Browse other questions tagged sqlsql-server-2012 or ask your own question.

Comments are closed.