Questions tagged [sp-executesql]

sp_executesql is a system stored procedure in Microsoft's SQL Server product range (2005 onwards) for running dynamic SQL.

sp_executesql is a system stored procedure in Microsoft's SQL Server product range (2005 onwards).

It is used for running dynamic T-SQL Statements or batches, including support for embedded parameters.

It is also what the .NET framework uses for ad-hoc queries when parameters are added to a SqlCommand.


Articles

235 questions
3
votes
1 answer

EXEC and Set Quoted_Identifier

I've got a Stored proc [A] that creates another stored proc [B] [A] Will never be run by end users and has no parameters or other untrusted data. Instead it is used by me simply to automate the create of the complex SP [B]. [A] Will always have the…
DJL
  • 2,060
  • 3
  • 20
  • 39
3
votes
3 answers

Incorrect syntax near '=' sp_executesql

I need to delete all rows in some table where value is empty string.(I have multiple table which got similar name). I tryed to execute those sql statement which is in string: DECLARE @sql AS NVARCHAR(MAX) DECLARE @emptyValue AS NVARCHAR(1) ='' …
Harry89pl
  • 2,415
  • 12
  • 42
  • 63
3
votes
2 answers

Is there an procedure in MySQL similar to sp_executesql in SQL Server?

I would like to make dynamic queries in within my procedure. SQL Server has neat sp_executesql procedure for such tasks, is there anything in MySQL which can help me to achieve similar functionality?
Deka
  • 131
  • 1
  • 4
  • 12
3
votes
3 answers

Access SQL Server temporary tables created in different scope

I am writing a stored procedure for SQL Server 2008 in which I need to extract information from a set of tables. I do not know ahead of time the structure of those tables. There is another table in the same database that tells me the names and types…
j0aqu1n
  • 1,013
  • 7
  • 14
2
votes
1 answer

Using stored procedures with LINQ

I want to use LINQ to call stored procedure in this way but stored procedure I want to call contain SQL string that executed by EXEC sp_executesql @strSQL In this way Visual Studio does not generate result class. To resolve this problem I execute…
shtkuh
  • 349
  • 1
  • 10
  • 22
2
votes
1 answer

sp_executeSQL and Statment with more than 2000 characters

I'm using dynamic SQL and I need to exec a long SQL query, First I declare @var with query as nvarchar(4000), but my query is longer than 4000 chars. I try to change to nvarchar(8000) but raise an Exception that can't be longer than 4000…
arturn
  • 725
  • 2
  • 11
  • 25
2
votes
1 answer

Dynamic sql and spexecutesql behavior

I have a table like c1 c2 c3 c4 ----------------- 2 1 7 13 9 2 8 14 1 3 9 15 5 4 10 16 2 5 11 17 11 6 12 18 As in general I would not know the number of columns (in the code @d here 4) to get…
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
2
votes
3 answers

Dynamic query results into a temp table or table variable

I have a stored procedure that uses sp_executesql to generate a result set, the number of columns in the result can vary but will be in the form of Col1 Col2 Col3 etc. I need to get the result into a temp table or table variable so I can work with…
2
votes
1 answer

How to map a SQL profiler captured 'exec sp_executesql' to stored procedure name in sql server

I have a huge SQL server database. I want to re-write an application. For that I want to reuse the database which has many tables, views and stored procedure. I am using SQL Profiler tool to map the application functionality to database. In…
2
votes
2 answers

EXEC sp_executesql - capture RETURN value and OUTPUT value from stored procedure call

Given a simple stored procedure, that populates a OUTPUT parameter and also RETURNs a value, such as: CREATE PROCEDURE sp_test ( @param_out INT OUTPUT ) AS BEGIN SELECT @param_out = 9 RETURN 2 END How can I call this procedure using…
devklick
  • 2,000
  • 3
  • 30
  • 47
2
votes
1 answer

Passing More then One Parameter to [sp_execute_external_script] for Executing R code

Here is the scenario where i am trying to pass three parameter namely empid2 and Name1 & DOB1. i am unable to pass the parameter and get the resultant data frame at the OutPutDataset. Observed : with One Parameter passing i am getting the result…
Nabi Shaikh
  • 787
  • 1
  • 6
  • 26
2
votes
1 answer

How to get sp_executesql result into a variable in dynamic query?

In the below procedure, the Select statement in the second IF statement returns results (e_id). But I want to store results of the select statement in one variable. I am deleting the records from temporary table and inserting results in temporary…
Ram
  • 727
  • 2
  • 16
  • 33
2
votes
2 answers

SQL Server : sp_executesql parameters not working

I have a VALID sp_executesql code, generated from C# ADO.NET, but the parameters are not passed to the stored procedure (SQL Server issue). This is what I found with SQL Profiler: declare @p3 StockSyncType insert into @p3 values(3, 17594, 73471,…
Pacurar Stefan
  • 235
  • 4
  • 9
2
votes
2 answers

T-SQL Create Table with Dynamically Named Database with sp_executesql

I am attempting to create a table in T-SQL using sp_executesql. The name of the database containing the table is dynamic. DECLARE @ID int = 1031460 DECLARE @TableName nvarchar(max) = '[MyDatabase' + CAST(@ID as nvarchar(10)) +…
Jacob Quisenberry
  • 1,131
  • 3
  • 20
  • 48
2
votes
4 answers

How to create sp_executesql to drop tables dynamicaly

For some reasons, I am trying to create a dynamic script to drop tables that I created before. I couldnt do the syntax right and I need help for this matter. When I run my script, it gives the error: "Procedure expects parameter '@statement' of…
Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189
1 2
3
15 16