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

Can a variable-value be passed into a local variable? Could the local vairable then be used to build the Dynamic SQL query?

I'm trying to pass user-inputted data to a SQL 'sp_executesql' (Dynamic SQL) statement in order to build a string for the 'SELECT','FROM', and 'WHERE' statements. I know that SQL Server won't accept a table name or a column name as a parameter.…
0
votes
1 answer

How to Get output from nested SP_executeSQL

Can you tell me how to insert executed variable @name in my table? I had some coding and this is what i managed to do but I do not know whats next: DECLARE @Name nvarchar(200); DECLARE @dbcatalog nvarchar(128); declare @sql nvarchar(4000) …
0
votes
1 answer

How to create stored procedure with view?

I tried to execute this code and it worked o.k without the the stored procedure and with it, it made an error. The error is: Msg 102, Level 15, State 1, Procedure UV_MTBF, Line 251 Incorrect syntax near 'Event_'. Is the stored procedure have a…
talR
  • 7
  • 2
  • 7
0
votes
0 answers

Run a dynamic script using the Execute sp_executesql statement with output to a variable via output

I have a query with dynamic variables that I arm or execute by means of an EXECUTE sp_executesql statement with output to a variable by means of the output clause but for some reason it enter code heres not possible that the result is store…
0
votes
3 answers

How to exec a create view with variables

I am trying to execute a view every time to procedure run. what is the problem with my code? why it doesn't work? CREATE PROCEDURE dbo.MTBFAlterView @PressType nvarchar(50), @TestName nvarchar(50), @PressName nvarchar(50), @Phase…
talR
  • 7
  • 2
  • 7
0
votes
2 answers

How to capture the result set of a stored procedure with a dynamic Select (sp_executesql) in a RDLC report

I need to be able to obtain the result set that will generate a stored procedure that has a dynamic query (sp_executesql), the problem is that when defining the DataSource, which in this case would be the stored procedure, it does not return any…
NHCives
  • 21
  • 7
0
votes
2 answers

Creating an Dynamic Tables in MS-SQL (feasibility)

Is there a way to create an array of tables (or dynamical created tables) based on a dynamic value? For example I know that in C++ you can use pointers and dynamic memory but i need to do something like this in SQL. A user specifies they want to…
0
votes
1 answer

How does sp_execute manage credentials when accessing a different database/server?

I have two databases, on two different servers, with different credentials to access them. I need to call a stored procedure in the second database from within the context of the first database. However, for security purposes, the Users are not…
Daniel Bragg
  • 1,773
  • 1
  • 12
  • 25
0
votes
1 answer

Assign result of a dynamic sql statement containing COALESCE to a variable

I have a dynamic SQL statement built using COALESCE. I need the result from the execution of this dynamic sql statement to be assigned to a variable. I tried the following code but it is not assigning the output to the variable @result. When…
0
votes
2 answers

Param error in where clause when executing sp_executesql

I searched for an answer but I didn't find anything. How to resolve this error? Msg 137, Level 15, State 2, Line 10 Must declare the scalar variable "@SDP". This is my code: declare @SQL NVARCHAR(4000), @dfilter NVARCHAR(100) …
sisty
  • 23
  • 2
  • 5
0
votes
1 answer

Sanitize dynamic SQL query created by user. only SELECT allowed (no INSERT,UPDATE,DELETE,DROP, EXEC, etc...)

I am developping an ASP2.0 website with a Microsoft SQL server 2005 Database. I need to implement a functionality which allows users to create a select query (nothing too complex) so that the website displays a page with the result set of the…
MDY
  • 3
  • 2
0
votes
1 answer

Error converting data type varchar to int dynamic SQL select statement

I am trying to execute sp_executesql inside a stored procedure. When I execute the stored procedure, it throws this following error Msg 8114, Level 16, State 5, Line 0 Error converting data type varchar to int I couldn't figure it out where the…
0
votes
2 answers

sp_executesql not declaring variable

I'm trying to do some dynamic SQL like so in order to create an audit log that just shows the changes. When I run that I'm getting an error stating Must declare the scalar variable "@OldValueOut". I thought I did declare that, via the second…
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
0
votes
1 answer

Is it better to use Update or execSQL to update data on SQLite?

I saw in a tutorial a code updating data from a SQlite database using execSQL: String update = "UPDATE FRUIT SET COLOR=? WHERE ID=?"; myDatabase.execSQL( update, new Object[] {"RED", 7}); Cursor cursor = myDatabase.rawQuery("SELECT *…
0
votes
1 answer

EXECUTE sp_executesql not working in SQL Server

I'm trying to implement a mechanism that would perform calculations based on dynamically defined algorithms. The way I do it is: Build a string containing the definition of all the variables along with their values, Fetch (from a table) the…
FDavidov
  • 3,505
  • 6
  • 23
  • 59