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
1 answer

HTML5 database table - check if empty

I'm attempting to write a function to determine if an html5 websql db table is empty. Code is below. I put alerts in there to see what is happening. When this function runs the alert at bottom pops up first. Although the table is empty the…
septemberbrain
  • 998
  • 9
  • 25
0
votes
1 answer

Why ERROR_LINE() returns 1 instead of exact line number in sql dynamic query execution

I executes numbers of dynamic queries in my procedure so if any error occurred in those query I want to know on which line no error occurred, I always get wrong line no which is 1, which is incorrect. I used ERROR_LINE() but it does not work with a…
0
votes
1 answer

SQL Server EXEC "sp_executesql" issues - the procedure returns value but the "select Output variable " statement returns a null value

I have the following code as part of A stored procedure where I try to send emails dynamically. I try to use the @Queryresult as part of an HTML table to be added to the email body but I can't get the @Queryresult value. `DECLARE @DataBaseName…
CedD
  • 3
  • 3
0
votes
1 answer

output variable from sp_executesql is always null, yet showing the value in SSMS

I'm trying to build a series of dynamic SQL calls based on the existence of a list of tables. I want to check if the table exists in the database and if so, update certain fields. However, when I run the following statement, @object_id is always…
Velocedge
  • 1,222
  • 1
  • 11
  • 35
0
votes
1 answer

sp_executesql not working from databricks - pyodc

Below is my sample databricks code where ACTUAL_TABLE and TEST_TABLE are SQL tables. I am trying to insert into ACTUAL_TABLE from TEST_TABLE in databricks using pyodc connection. mssql_db.sql is my connection and the connection is working as…
pythonCoder
  • 25
  • 1
  • 5
0
votes
0 answers

How to use a variable in Openrowset command With SQL Commands

I want to update database from a .sql file which contain some commands like update and edit and delete. I try hard to pass the .sql file to the procedure to make it run. This is my code which only shows me the file instead of running it: DECLARE…
0
votes
1 answer

SQL Server - EXECUTE permission

In our Azure Managed Instance DB, a new user has been created and given db_datareader, db_datawritter and db_ddladmin. As per MS Documentation, db_ddladmin has CREATE PROCEDURE and ALTER ANY SCHEMA. But when logged in, I am unable to EXECUTE SPs and…
0
votes
0 answers

Output parameter from TSQL sp_executesql seems to be null

I'm sure there's a simple explanation. I'm trying to print out the Max ID for all tables with an Identity column. The TSQL code below seems to run fine in the sense that the Results window contains cells with values, but the Messages window does not…
Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
0
votes
1 answer

How can I concatenate stringvalues in a parameter definition for a sp_executesql statement?

I'm trying to concatenate string parameters to a sp_executesql statement but I can't find a way to do it. It's quite possible I'm not searched the net worth the right words. What I'm trying to to can be exemplified with the following code, where I…
MarJer
  • 87
  • 2
  • 9
0
votes
1 answer

sp_execute_remote sometimes fails to return - why? / workarounds

I have an extensive Azure SQL stored proc, called periodically from Azure Data Factory, which is querying another Azure SQL database in the same region using sp_execute_remote. 99.5% of the time this works fine but, on some occasions, the call to…
Intention
  • 1
  • 1
0
votes
1 answer

Hide Result set of sp_executesql

I am trying to run sp_executesql but I don't want to show the results in resultset and I can't use variables because my stored procedure calls different datasources. So I am not able to create single table what should I do? Any suggestions? --INSERT…
0
votes
3 answers

Does sp_executesql support multiple values in one parameter and return multiple records?

I have created a stored procedure as shown below, but it's returning only one row instead of 3: CREATE PROCEDURE [dbo].[tempsp] (@RecycleIds NVARCHAR(MAX) = NULL) AS BEGIN DECLARE @Err INT DECLARE @WhereClause NVARCHAR(MAX) …
0
votes
1 answer

Update a null column in sql with spexecutesql

I encountered a problem in the following query. When I go to update the column ID_Premesso with the value NULL the value does not change, while if I go to update with the value 0 it works. SET @SQL = 'UPDATE ' + @NameTB + ' SET ID_Permesso =…
Rosko
  • 1
0
votes
0 answers

T-SQL stored procedure output parameter

I have a stored procedure called R_Proc with a parameter called id. Inside I am creating temp table ##Insert_Data for inserting data: set @Sql_Create_Table = 'CREATE TABLE ##Insert_Data...' exec sp_executesql @Sql_Create_Table Then I have created a…
Nick
  • 3
  • 3
0
votes
1 answer

How to run query with parameters against a database whose name is in a variable using sp_ExecuteSql

I am working with Microsoft SQL Server 2016 in Windows Server 2016. I have a query with 1 parameter, string type. select PH.project_name, PD.employee_id, E.first_name, E.last_name from project_header PH inner join project_detail PD on…
srh
  • 1,661
  • 4
  • 30
  • 57