sp-executesql

T-SQL EXEC versus sp_exec

醉酒当歌 提交于 2019-12-13 04:47:31
问题 I tried the following: declare @var2 nvarchar(30) declare @qsql nvarchar(100) set @var2 = N'iddelegat' exec ('select max('+ @var2 + ') as IDexec from delegat'); set @qsql = 'select max(@varsp) as IDspexec from delegat'; exec sp_executesql @qsql, N'@varsp nvarchar(30)', @var2; And the result: IDexec IDspexec ----------------------- 500038 iddelegat I could not understand why sp_executesql does not return the same result as EXECUTE . The right returned values are only in EXECUTE statement. It

Fully qualified table names with SP_ExecuteSql to access remote server

倖福魔咒の 提交于 2019-12-13 03:32:37
问题 Trying to update a table on a linked server (SQL 2000/2005) but my server name will not be known ahead of time. I'm trying this: DECLARE @Sql NVARCHAR(4000) DECLARE @ParamDef NVARCHAR(4000) DECLARE @SERVER_NAME VARCHAR(35) SET @Sql = 'UPDATE @server_name_param.dba_sandbox.dbo.SomeTable SET SomeCol=''data''' SET @ParamDef = N'@server_name_param VARCHAR(35)' print @Sql exec sp_executesql @Sql, @ParamDef, @server_name_param=@SERVER_NAME Which returns this: UPDATE @server_name_param.dba_sandbox

Incorrect syntax near '=' sp_executesql

你说的曾经没有我的故事 提交于 2019-12-10 19:56:01
问题 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) ='' set @sql = N'DELETE FROM SampleTable WHERE Value='+@emptyValue+'' exec sp_executesql @sql But it's throw me error Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '='. I tryed to figure it out about an hour now. Any help would be appreciated.

Using LIKE in sp_executesql

眉间皱痕 提交于 2019-12-05 06:23:06
SET @whereCond = @whereCond + ' AND name LIKE ''%'' + @name + ''%''' Is there something wrong here? After I generate where condition, I execute it with sp_executesql , but I did get anything. When I SELECT the same thing without sp, it's ok. How to use LIKE in sp_executesql? Can you bring some examples, please? Thank you. UPDATE declare @name nvarchar(50) set @name = 'a' SELECT * FROM Tbl_Persons WHERE 1 = 1 AND lastname LIKE '%a%' exec sp_executesql N'SELECT * FROM Tbl_Persons WHERE 1 = 1 AND lastname LIKE ''%@name%''', N'@name nvarchar(50)', @name=@name First query returns values, second one

Is COMMIT required after every EXECUTE IMMEDIATE?

一世执手 提交于 2019-12-05 05:24:43
I have multiple EXECUTE IMMEDIATE commands within one oracle procedure. EXECUTE IMMEDIATE 'DELETE FROM tbl1'; EXECUTE IMMEDIATE 'INSERT INTO tbl1...'; COMMIT; EXECUTE IMMEDIATE 'DELETE FROM tbl3'; EXECUTE IMMEDIATE 'INSERT INTO tbl3 ...'; COMMIT; EXECUTE IMMEDIATE 'DELETE FROM tbl4'; EXECUTE IMMEDIATE 'INSERT INTO tbl4 ...'; COMMIT; Do I need all of these COMMIT, or just at the end of the procedure? The only times that you're really forced to commit, other thasn at the end of a business transaction, are: When executing DDL: the DDL execution is wrapped in a pair of implicit commits. After

EXEC and Set Quoted_Identifier

≯℡__Kan透↙ 提交于 2019-12-04 05:05:11
问题 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 same result unless it's internals are changed. Therefore I consider this to be safe. [B] requires Quoted_Identifiers ON as it uses xml. If I copy and paste the generated SP it works fine but if I let [A] create it with EXEC then the SP fails when

Easy way to convert exec sp_executesql to a normal query?

岁酱吖の 提交于 2019-12-02 17:32:06
When dealing with debugging queries using Profiler and SSMS, its pretty common for me to copy a query from Profiler and test them in SSMS. Because I use parameterized sql, my queries are all sent as exec sp_executesql queries. exec sp_executesql N'/*some query here*/', N'@someParameter tinyint', @ someParameter =2 I'll take this and convert it into a normal query for ease of editing (intellisense, error checking, line numbers, etc): DECLARE @someParameter tinyint SET @someParameter = 2 /*some query here*/ Of course, the bigger and more complex the query, the harder to do this. And when you're

Dynamic query results into a temp table or table variable

萝らか妹 提交于 2019-12-02 02:27:58
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 it. The problem is I need to define the columns of the temp table, which I cant do dynamically using sp_executesql as the scope of the temp table is lost after the command is executed. I have toyed with the idea of using Global Temp tables, as the scope allows it to be created dynamically, however, there is a very good chance the Global Temps would

How to use sp_executesql to avoid SQL Injection

时间秒杀一切 提交于 2019-12-02 02:03:54
In the below sample code, Table Name is an input parameter. In this case, how can I avoid SQL injection using sp_executesql . Below is the sample code, I am trying to use sp_executesql to avoid it but it doesn't work. Can anyone tell me how to correct it? ALTER PROC Test @param1 NVARCHAR(50), @param2 INT, @tblname NVARCHAR(100) AS BEGIN DECLARE @sql NVARCHAR(1000) SET @sql= N' select * from ' + @tblname + ' where name= @param1 and id= @param2'; PRINT @sql EXEC Sp_executesql @sql, N'@param1 nvarchar(50), @param2 int', @param1, @param2; END EXEC Test 'John', 2, ' emp; delete from emp where id =

Dynamic query results into a temp table or table variable

我的梦境 提交于 2019-12-02 01:45:22
问题 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 it. The problem is I need to define the columns of the temp table, which I cant do dynamically using sp_executesql as the scope of the temp table is lost after the command is executed. I have toyed with the idea of using Global Temp tables, as the