dynamic-sql

Select columns with the same prefix [duplicate]

女生的网名这么多〃 提交于 2020-01-06 17:27:25
问题 This question already has answers here : Use text output from a function as new query (2 answers) Closed 4 years ago . Using the following code I can select a few columns that share the same prefixes (either upreg_srt or downreg_srt) from my table and delete (drop) them: DO $do$ DECLARE _column TEXT; BEGIN FOR _column IN SELECT DISTINCT quote_ident(column_name) FROM information_schema.columns WHERE table_name = 'all_se_13patients_downreg_ranks' AND column_name LIKE '%upreg_srt' OR column_name

Error in Dynamic SQL SP

让人想犯罪 __ 提交于 2020-01-06 08:24:13
问题 I Have created a procedure which has code like this: Create PROCEDURE Sample( @ID INT ) AS BEGIN DECLARE @SQL NVARCHAR(max) DECLARE @SchemaName SYSNAME DECLARE @TableName SYSNAME DECLARE @DatabaseName SYSNAME SELECT @SQL = 'Create Table ' + @DatabaseName + '.' + @SchemaName + '.' + @TableName + '_temp' + '(' SELECT @SQL = @SQL + 'ID int NOT NULL Primary Key, Name VarChar(10))' I Always get error as : Msg 102, Level 15, State 1, Line 77 Incorrect syntax near ','. Can anyone help me on this?

How to return result of many select statements as one custom table

拈花ヽ惹草 提交于 2020-01-06 08:24:11
问题 I have a table (let's name it source_tab) where I store list of all database tables that meet some criteria. tab_name: description: table1 some_desc1 table2 some_desc2 Now I need to execute a select statement on each of these tables and return a result as a table (I created custom TYPE). However I have a problem - when using bulk collect, only the last select statement is returned. The same issue was with open cursor. Is there any possibility to achieve this goal, another then concatenating

if-statement with string containing the condition

 ̄綄美尐妖づ 提交于 2020-01-05 14:14:12
问题 This question is about Postgresql 8.3 . I've got a table with a field containing conditions like 'lastcontact is null'. In code, I want to loop through this table and for each record, I want to check 'if condition then', like in this example: FOR myrec IN SELECT * FROM tabel ORDER BY colorlevel, volgnummer LOOP if (myrec.conditie) then raise notice 'Condition % is true', myrec.conditie; else raise notice 'Condition % is false', myrec.conditie; end if; END LOOP; The table which I have called

if-statement with string containing the condition

空扰寡人 提交于 2020-01-05 14:13:30
问题 This question is about Postgresql 8.3 . I've got a table with a field containing conditions like 'lastcontact is null'. In code, I want to loop through this table and for each record, I want to check 'if condition then', like in this example: FOR myrec IN SELECT * FROM tabel ORDER BY colorlevel, volgnummer LOOP if (myrec.conditie) then raise notice 'Condition % is true', myrec.conditie; else raise notice 'Condition % is false', myrec.conditie; end if; END LOOP; The table which I have called

sql update with dynamic column names

萝らか妹 提交于 2020-01-05 03:42:48
问题 EDIT: Database names have been modified for simplicity I'm trying to get some dynamic sql in place to update static copies of some key production tables into another database (sql2008r2) . The aim here is to allow consistent dissemination of data (from the 'static' database) for a certain period of time as our production databases are updated almost daily. I am using a CURSOR to loop through a table that contains the objects that are to be copied into the 'static' database. The prod tables

Dynamically access column value in record

只谈情不闲聊 提交于 2020-01-05 02:48:19
问题 Is it possible to dynamically access a column value from a record by its name? I'm writing a trigger function that executes a dynamic SQL command and I would like to dynamically extract a column value from a NEW record by column name. Here's a simplified example of what I'm trying to do: $$ DECLARE command text := 'UPDATE $1 SET $2 = $3'; myColumn := 'votes' BEGIN EXECUTE command using 'anotherTable', myColumn, NEW.myColumn; END $$ 回答1: That's possible , but the USING clause of EXECUTE can

Dynamic SQL with variables inside a view (SQL Server)

末鹿安然 提交于 2020-01-04 15:30:25
问题 Hello I'm essentially trying to do this inside a new view window in SQL Server 2008: declare @var = (select db from databases); exec ('select name from ' + @var ' + .dbo.Names); This view actually runs in SQL Server but I cannot save it (it gives me an error), I could potentially just create a table returning function, do all of this same stuff in it and return the table and create a view that basically takes everything from that table but I was unsure of performance hits that could occur

Dynamic SQL and stored procedure optimization

不羁岁月 提交于 2020-01-04 09:29:27
问题 I've read that using Dynamic SQL in a stored procedure can hurt performance of your stored procedures. I guess the theory is that the store procedure won't store an execution plan for SQL executed via EXEC or sp_executesql. I want to know if this is true. If it is true, do I have the same problem with multiple nested IF blocks, each one with a different "version" of my SQL statement? 回答1: If you have multiple nested IF blocks then SQL Server will be able to store execution plans. I'm assuming

Can MySQL triggers be created with dynamic SQL from within a stored procedure?

萝らか妹 提交于 2020-01-03 10:57:26
问题 Is it possible to create a trigger in MySQL using dynamically generated SQL from within a stored procedure? I am executing other dynamically constructed queries in my procedure by preparing a statement, but when I try the same approach to create a trigger I get the following error: ERROR Code: 1295This command is not supported in the prepared statement protocol yet From Bug #31625, PREPARED STATEMENT syntax does not allow to create TRIGGERS I see other people have been complaining about the