dynamic-sql

Why does running this query with EXECUTE IMMEDIATE cause it to fail?

只愿长相守 提交于 2019-12-17 20:06:10
问题 I am writing a PL/SQL procedure that needs to to dynamically generate some queries, one of which involves creating a temporary table using results from a query taken as a parameter. CREATE OR REPLACE PROCEDURE sqlout(query IN VARCHAR2) IS BEGIN EXECUTE IMMEDIATE 'CREATE GLOBAL TEMPORARY TABLE tmp_tab AS (' || query || ');'; END; It compiles correctly, but even with very simple queries such as with: BEGIN sqlout('SELECT * FROM DUAL'); END; IT throws ORA-00911: invalid character . If I run the

Format specifier for integer variables in format() for EXECUTE?

梦想的初衷 提交于 2019-12-17 19:44:21
问题 CREATE OR REPLACE FUNCTION getParentLtree(parent_id bigint, tbl_name varchar) RETURNS ltree AS $BODY$ DECLARE parent_ltree ltree; BEGIN -- This works fine: -- select into parent_ltree l_tree from tbl1 where id = parent_id; EXECUTE format('select into parent_ltree l_tree from %I where id = %I', tbl_name,parent_id); RETURN parent_ltree; END; $BODY$ LANGUAGE plpgsql; There are 2 issues in above function: parent_id is integer but it is replaced with quotes? What is the correct format specifier

How to unpivot a table in PostgreSQL

时光怂恿深爱的人放手 提交于 2019-12-17 17:06:49
问题 I am having difficulties writing a Postgres function, as I am not familiar with it. I have multiple tables to import into Postgres with this format: id | 1960 | 1961 | 1962 | 1963 | ... ____________________________________ 1 23 45 87 99 2 12 31 ... which I need to convert into this format: id | year | value _________________ 1 1960 23 1 1961 45 1 1962 87 ... 2 1960 12 2 1961 31 ... I would imagine the function too to read like this: SELECT all-years FROM imported_table; CREATE a new_table;

Error when setting n_distinct using a plpgsql variable

懵懂的女人 提交于 2019-12-17 14:54:54
问题 I tried to use a function to set the n_distinct value for a table. The code is as follows: create temporary table _temp ( id integer ); create function pg_temp.setdistinct(_cnt real) returns void as $$ begin alter table _temp alter column id set (n_distinct=_cnt); end $$ language plpgsql; select pg_temp.setdistinct(1000); Yet receive the following errors: ERROR: invalid value for floating point option "n_distinct": _cnt CONTEXT: SQL statement "alter table _temp alter column id set (n_distinct

Why do I get “Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.” when I try to use sp_executesql?

南笙酒味 提交于 2019-12-17 10:57:19
问题 Why do I get this error Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'. when I try to use sp_executesql? 回答1: Sounds like you're calling sp_executesql with a VARCHAR statement, when it needs to be NVARCHAR. e.g. This will give the error because @SQL needs to be NVARCHAR DECLARE @SQL VARCHAR(100) SET @SQL = 'SELECT TOP 1 * FROM sys.tables' EXECUTE sp_executesql @SQL So: DECLARE @SQL NVARCHAR(100) SET @SQL = 'SELECT TOP 1 * FROM sys.tables' EXECUTE sp_executesql @SQL

How to select column names dynamically in mySQL

筅森魡賤 提交于 2019-12-17 07:54:12
问题 I want to select column names but I don't know the table structure ahead of time and it may change so I can't just hard code the select statement with column names. I also do NOT want to select every column. Is there and easy way to do this? My thoughts are it's some kind of combination of these two queries but my SQL is not that good. SHOW COLUMNS FROM table_name; SELECT * FROM table_name; I tried using a sub select but it didn't work. Nothing seems to happen, I don't get an error I just get

T-SQL: How to use parameters in dynamic SQL?

拜拜、爱过 提交于 2019-12-17 07:37:12
问题 I have the following dynamic query which is working fine without the WHERE clause, which is expecting UNIQUEIDENTIFIER . When I pass it in, I don't get a result. I tried CAST and CONVERT , but no result. I might be doing it wrong, can anybody help? CREATE PROCEDURE [dbo].[sp_Test1] /* 'b0da56dc-fc73-4c0e-85f7-541e3e8f249d' */ ( @p_CreatedBy UNIQUEIDENTIFIER ) AS DECLARE @sql NVARCHAR(4000) SET @sql =' DECLARE @p_CreatedBY UNIQUEIDENTIFIER SELECT DateTime, Subject, CreatedBy FROM ( SELECT

T-SQL: How to use parameters in dynamic SQL?

拈花ヽ惹草 提交于 2019-12-17 07:37:12
问题 I have the following dynamic query which is working fine without the WHERE clause, which is expecting UNIQUEIDENTIFIER . When I pass it in, I don't get a result. I tried CAST and CONVERT , but no result. I might be doing it wrong, can anybody help? CREATE PROCEDURE [dbo].[sp_Test1] /* 'b0da56dc-fc73-4c0e-85f7-541e3e8f249d' */ ( @p_CreatedBy UNIQUEIDENTIFIER ) AS DECLARE @sql NVARCHAR(4000) SET @sql =' DECLARE @p_CreatedBY UNIQUEIDENTIFIER SELECT DateTime, Subject, CreatedBy FROM ( SELECT

sql use statement with variable

点点圈 提交于 2019-12-17 07:31:17
问题 I'm trying to switch the current database with a SQL statement. I have tried the following, but all attempts failed: USE @DatabaseName EXEC sp_sqlexec @Sql -- where @Sql = 'USE [' + @DatabaseName + ']' To add a little more detail. EDIT: I would like to perform several things on two separate database, where both are configured with a variable. Something like this: USE Database1 SELECT * FROM Table1 USE Database2 SELECT * FROM Table2 回答1: exec sp_execsql @Sql The DB change only lasts for the

sql use statement with variable

删除回忆录丶 提交于 2019-12-17 07:31:11
问题 I'm trying to switch the current database with a SQL statement. I have tried the following, but all attempts failed: USE @DatabaseName EXEC sp_sqlexec @Sql -- where @Sql = 'USE [' + @DatabaseName + ']' To add a little more detail. EDIT: I would like to perform several things on two separate database, where both are configured with a variable. Something like this: USE Database1 SELECT * FROM Table1 USE Database2 SELECT * FROM Table2 回答1: exec sp_execsql @Sql The DB change only lasts for the