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
0
votes
2 answers

C# how to call “sp_executesql” for bulk insert

I have files whic needs to be loaded to db with win form application for that i am creationg bulkload string C# code private void bt_loadInToTable_Click(object sender, EventArgs e) { bulkinsert = " BULK INSERT " +…
Andrey
  • 1,629
  • 13
  • 37
  • 65
0
votes
1 answer

OUTPUT variables to sp_executesql

Background: SQL Server 2008 R2 Having issues with the following. Been given a usp to "finish off", author is unavailable. It extracts data from source table, copies to target table of same name + datetime stamp in an archive db then truncates source…
0
votes
2 answers

EXEC sp_executesql doesn't work or doesn't return data

I have code part from trigger. When I test my trigger my exec doesn't return any data. I declared my exec value as: declare @sql nvarchar(MAX), @SQLString varchar(MAX) and here is the select: set @sql = N'select ' set @sql = @sql…
Ange
  • 5
  • 1
  • 3
0
votes
1 answer

SQL SP_EXECSQL @VAR to run a "dynamic" OpenQuery

I have an OpenQuery (Used in SQL2005 to run a query against a legacy database). I'm building the string to run so that I'll return the last 6 months of data. The problem I'm having is with '||' to concat. This works on the legacy system: SELECT …
WernerCD
  • 2,137
  • 6
  • 31
  • 51
0
votes
1 answer

How to get custom SQL Server type into Entity Framework generated code

There is a TABLE Type defined in SQL server: CREATE TYPE RealtySearchResult AS TABLE ( realtyId int not null, OwnerId int not null, ...) And stored procedure: CREATE PROCEDURE [dbo].[SearchRealty] (@fulltext nvarchar(200) null, @skipRows int, …
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
0
votes
1 answer

Create a login in stored procedure with null password using create login query?

I am not able to correct my query, I want to create login by passing parameter to the create login query with null password? ALTER PROCEDURE usp_login (@DLName VARCHAR(50), @DBName VARCHAR(50)) AS BEGIN DECLARE @statement…
Iceman
  • 3
  • 3
0
votes
1 answer

Use Temp Table to merge query results from different DBs

I need to extract data from different DBs into a single table. These DBs are all in the same Server and Instance and they have the same structure. One of the columns will be DB Name, the others come from the same table. I could write a query that…
0
votes
1 answer

Execute SQL Task with multiple stored procedures with input and out parameter

I have an "Execute SQL Task" with ONE stored procedure which needs to be called multiple times. This stored procedure takes one input value and returns one output value. So my code is like : EXEC test.sp_workdone ?,'TableName' EXEC…
RBK4YOU
  • 13
  • 4
0
votes
0 answers

SQL Server 2016 sp_executesql odd behavior (really)

I know there are lots of "odd behavior" questions on here, but I do think this is unique and I'm wondering if anyone has encountered this before? I'm building a large dynamic query in a single nvarchar variable, @SQL, and then executing it ... it's…
iwells
  • 1
0
votes
1 answer

Passing comma separated values to sp_executesql

I'm trying to use sp_executesql by passing CSV parameter. When I use EXEC by passing constructed string, it returns me some data declare @Accts nvarchar(100) = 'IntYTD,TotalIncome,PayoffYTD' declare @sql nvarchar(max) set @sql = 'select…
FLICKER
  • 6,439
  • 4
  • 45
  • 75
0
votes
2 answers

How to get Query Plan Reuse in MS SQL Server

I inherited a database application that has a table with about 450 queries. There's a calling procedures takes the @QueryId and @TheId as input parameters. The only way these queries are executed is via this procedure. The queries are like…
0
votes
1 answer

inset into exec dbo.sp_executesql (multiple statement string)

I use this code: select @SQLString=N'select ....(long select query).....' + char(10) + '; drop table _item_list ' and afterwards insert @tbl EXEC dbo.sp_executesql @SQLString It seems to be working, but can someone give me some clear info on…
George Menoutis
  • 6,894
  • 3
  • 19
  • 43
0
votes
1 answer

Azure SQL sp_execute_remote not supporting Output Parameters

I'm following to execute the Store Procedures Across the different Azure DB from here. The thing is my Original Sp has 2 output parameters. But when i tried to call the Sp from Other DB. I get the error as Procedure SP_EXECUTE_REMOTE, Line 1 [Batch…
Jayendran
  • 9,638
  • 8
  • 60
  • 103
0
votes
2 answers

Struggling to learn sp_executesql in SQL Server

I'm trying to teach myself both SQL Cursors and sp_executesql for a project I'm working on. I'm quite close, I think, but I'm failing to get what I want, and I'm hoping someone can help get me past my lack of knowledge. Thanks in advance for any…
0
votes
1 answer

sp_executesql Cross Server Insert return Scope_Identity()

I have found answers to my question, they all state that the below syntax should work. It does not work for me. My output variable is always NULL so any help would be appreciated. DECLARE @SqlCommand NVARCHAR(1000), @ParamDefinition…
Alwelder
  • 1
  • 2