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
1
vote
2 answers

SqlSever: Multiple CTE in EXECUTE SP_EXECUTESQL

DECLARE @sqlString nvarchar(500); SET @sqlString = ';WITH Data_CTE AS ( SELECT pj.ProjectID, pj.ProjectName, pj.ProjectOwner, cs.CustomerName FROM Projects as pj LEFT OUTER JOIN [Customers]…
Manas Kumar
  • 2,411
  • 3
  • 16
  • 23
1
vote
1 answer

sp_executesql sets a column to null

I'm attempting to execute some SQL inside of sp_executesql. Here is the generated SQL: exec sp_executesql N'declare @RC int EXECUTE @RC = [dbo].[sp_StoredProcedureName] @parameterName select @RC', N'@parameterName…
Jessica
  • 1,621
  • 2
  • 18
  • 34
1
vote
1 answer

C# how to call “sp_executesql” with custom parameters and sql body

I am trying to execute a dynamic SQL from C# and I want to use sp_executesql because I want to pass a list of strings to be used for an IN statement in the dynamic SQL passed to sp_executesql. The thing is that I don't know exactly how to do…
Alecu
  • 2,627
  • 3
  • 28
  • 51
1
vote
3 answers

Delete records dynamically from a SQL Server table

I have a table called Name_Tables that contains the names of other tables. Some of that tables have names like dbo.companyname.test and others dbo.testtest. For all these tables, mentioned in the table Name_Tables, I would like to delete the data,…
malue
  • 59
  • 2
  • 10
1
vote
0 answers

sp_executesql not running sql using 'with' clause

in work we use company-based test-framework. You just save your testing query into specific column in specific table, then execute the stored procedure, which calls sp_executesql to execute the query, then it logs the results and that all. Today…
1
vote
0 answers

How to execute the SQL Select Statement stored as Column value in a table using SSIS OLEDB Source Data Flow task

I am new to SSIS. Trying to execute the Select Statement query stored in table's column value using SSIS OLEDB Source Component, as we have written the query directly in SSIS package. If my Select Statement is 'Select col1, col2, col3 from Table1…
1
vote
1 answer

Declaring output parameters for sp_executesql

I'm trying to make a bit of SQL script that will output all the table names in a large database, along with the numbers of fields and records in each, and a list of the field names. This will allow us to focus on the tables with data, and look for…
1
vote
1 answer

sp_executesql or execute without variables

Id like to execute this query on sql server 2012 without using variables: DECLARE @query VARCHAR(4000) set @query= concat('select * from customer where id in (', (select id from customer where comid=1 and code='30.00.0000'), ') order by…
1
vote
1 answer

variables in sp_executesql

I have a piece of code: DECLARE @v int; DECLARE @SQLString nvarchar(500); DECLARE @ParmDefinition nvarchar(500); DECLARE @max_title varchar(30); SET @IntVariable = 197; SET @SQLString = N'SELECT @max_titleOUT = max(JobTitle) FROM…
Just a learner
  • 26,690
  • 50
  • 155
  • 234
1
vote
0 answers

Stored procedure output using sp_executesql returning null

I'm using the below procedure taking table name, schema name and database name dynamically to generate a SELECT statement. When I execute this stored procedure without assigning @select_statementOUT anywhere, it works fine. However, the OUTPUT…
1
vote
2 answers

SQL - query as parameter

I have code: DECLARE @sqlQuery NVARCHAR(4000); DECLARE @stn NVARCHAR(4000); SET @stn=N'SELECT cast(isnull(SUBSTRING(CDN.DokSumT(16,739684,1045,1,32768,1,1,739684,1,2,1,0,78877,0,3,1,1,0,0,0,0,0,-1) , 9 , 7 ),0) as decimal (28,4))'; SELECT @sqlQuery…
ngLucas
  • 43
  • 7
1
vote
3 answers

sp_executesql: Add column with case statement

This is an example code of what I would like to achieve, I want to add a column to a table with some specific code variables. However, I can't really grasp how the sp_executesql passes it through. Any help appreciated DECLARE @SQLQUERY NVARCHAR…
user3052850
  • 178
  • 3
  • 17
1
vote
1 answer

OPENROWSET: sp_executesql statement failing at @param

I am developing a program to pull in the XML file that WordPress enables you to download (essentially a backup copy). At this point I was automating the process to allow frequent backup of my data on SQL Server, and for some reason I am stuck at…
clifton_h
  • 1,298
  • 8
  • 10
1
vote
2 answers

sp_executesql code generated by reporting services return wrong values

I have used stored procedure with parameters to generate the result set for my report in reporting services 2012 but the result returned is wrong. I traced the command generated and here it is: EXEC sp_executesql N'EXECUTE…
SQLCH
  • 21
  • 1
1
vote
1 answer

Nested sp_executesql not working with output variable

I am trying to call a stored procedure (with output variable) using sp_executesql but within another stored procedure. I wrote the following, but still not able to get trhough what that error means This would be called from webservice code: exec sp1…