dynamic-sql

T-SQL Dynamic SQL and Temp Tables

筅森魡賤 提交于 2019-11-26 11:18:36
问题 It looks like #temptables created using dynamic SQL via the EXECUTE string method have a different scope and can\'t be referenced by \"fixed\" SQLs in the same stored procedure. However, I can reference a temp table created by a dynamic SQL statement in a subsequence dynamic SQL but it seems that a stored procedure does not return a query result to a calling client unless the SQL is fixed. A simple 2 table scenario: I have 2 tables. Let\'s call them Orders and Items. Order has a Primary key

Ad hoc queries vs stored procedures vs Dynamic SQL [closed]

醉酒当歌 提交于 2019-11-26 10:57:17
问题 Ad hoc queries vs stored procedures vs Dynamic SQL. Can anyone say pros and cons? 回答1: Stored Procedures Pro: Good for short, simple queries (aka OLTP--i.e. add, update, delete, view records) Pro: Keeps database logic separate from business logic Pro: Easy to troubleshoot Pro: Easy to maintain Pro: Less bits transferred over network (i.e. only the proc name and params) Pro: Compiled in database Pro: Better security (users don't need direct table access) Pro: Excellent query plan caching (

Using a cursor with dynamic SQL in a stored procedure

点点圈 提交于 2019-11-26 10:26:59
问题 I have a dynamic SQL statement I\'ve created in a stored procedure. I need to iterate over the results using a cursor. I\'m having a hard time figuring out the right syntax. Here\'s what I\'m doing. SELECT @SQLStatement = \'SELECT userId FROM users\' DECLARE @UserId DECLARE users_cursor CURSOR FOR EXECUTE @SQLStatment --Fails here. Doesn\'t like this OPEN users_cursor FETCH NEXT FROM users_cursor INTO @UserId WHILE @@FETCH_STATUS = 0 BEGIN EXEC asp_DoSomethingStoredProc @UserId END CLOSE

Dynamic SQL results into temp table in SQL Stored procedure

无人久伴 提交于 2019-11-26 09:38:47
问题 The code is as follows: ALTER PROCEDURE dbo.pdpd_DynamicCall @SQLString varchar(4096) = null AS Begin create TABLE #T1 ( column_1 varchar(10) , column_2 varchar(100) ) insert into #T1 execute (\'execute \' + @SQLString ) select * from #T1 End The problem is that I want to call different procedures that can give back different columns. Therefore I would have to define the table #T1 generically. But I don\'t know how. Can anyone help me on this problem? 回答1: Try: SELECT into #T1 execute (

Dynamically generate columns for crosstab in PostgreSQL

做~自己de王妃 提交于 2019-11-26 09:01:12
问题 I am trying to create crosstab queries in PostgreSQL such that it automatically generates the crosstab columns instead of hardcoding it. I have written a function that dynamically generates the column list that I need for my crosstab query. The idea is to substitute the result of this function in the crosstab query using dynamic sql. I know how to do this easily in SQL Server, but my limited knowledge of PostgreSQL is hindering my progress here. I was thinking of storing the result of

Generate Delete Statement From Foreign Key Relationships in SQL 2008?

£可爱£侵袭症+ 提交于 2019-11-26 07:27:47
问题 Is it possible via script/tool to generate a delete statement based on the tables fk relations. i.e. I have the table: DelMe(ID) and there are 30 tables with fk references to its ID that I need to delete first, is there some tool/script that I can run that will generate the 30 delete statements based on the FK relations for me ? (btw I know about cascade delete on the relations, I can\'t use it in this existing db) I\'m using Microsoft SQL Server 2008 回答1: DELETE statements generated for use

Why cannot I use bind variables in DDL/SCL statements in dynamic SQL?

☆樱花仙子☆ 提交于 2019-11-26 04:54:01
问题 I am trying to execute an SQL command within dynamic SQL with bind variables: -- this procedure is a part of PL/SQL package Test_Pkg PROCEDURE Set_Nls_Calendar(calendar_ IN VARCHAR2) IS BEGIN EXECUTE IMMEDIATE \'ALTER SESSION SET NLS_CALENDAR = :cal\' USING IN calendar_; END Set_Nls_Calendar; Then on the client side, I am trying to invoke the procedure: Test_Pkg.Set_Nls_Calendar(\'Thai Buddha\'); But this get\'s me ORA-02248: invalid option for ALTER SESSION . And my question is: Why cannot I

Define table and column names as arguments in a plpgsql function?

允我心安 提交于 2019-11-26 04:51:28
问题 It must be simple, but I\'m making my first steps into Postgres functions and I can\'t find anything that works... I\'d like to create a function that will modify a table and / or column and I can\'t find the right way of specifying my tables and columns as arguments in my function. Something like: CREATE OR REPLACE FUNCTION foo(t table) RETURNS void AS $$ BEGIN alter table t add column c1 varchar(20); alter table t add column c2 varchar(20); alter table t add column c3 varchar(20); alter

PostgreSQL parameterized Order By / Limit in table function

我的未来我决定 提交于 2019-11-26 04:51:00
问题 I have a sql function that does a simple sql select statement: CREATE OR REPLACE FUNCTION getStuff(param character varying) RETURNS SETOF stuff AS $BODY$ select * from stuff where col = $1 $BODY$ LANGUAGE sql; For now I am invoking this function like this: select * from getStuff(\'hello\'); What are my options if I need to order and limit the results with order by and limit clauses? I guess a query like this: select * from getStuff(\'hello\') order by col2 limit 100; would not be very

nvarchar(max) still being truncated

守給你的承諾、 提交于 2019-11-26 04:45:54
问题 So I\'m writing a stored procedure in MS SQL Server 2008. It\'s a really long query and I have to write it dynamically, so I create a variable called @Query and make it of type NVARCHAR(MAX) . Now, I have been told that in modern versions of SQL Server, NVARCHAR(MAX) can hold a ridiculous amount of data, way more than the original 4000 character maximum. However, @Query is still getting truncated to 4000 characters when I try to print it out. DECLARE @Query NVARCHAR(max); SET @Query = \