dynamic-sql

Error when trying to execute a command in sql server

前提是你 提交于 2020-04-20 18:40:47
问题 I am using SQL Server 2014 and working on creating views from all the user database on to a Test database. The generated sql statement works fine when run from SSMS but when the execute command tries to run it I get the following error message. SQL Query: DECLARE @SQL NVARCHAR(MAX), @QueryStmt NVARCHAR(MAX) SET @SQL = '' SELECT @SQL = @SQL + CHAR(13) + 'USE ' + QUOTENAME([name]) + '; SELECT @QueryStmt = ''USE TestDWH ''+ CHAR(13) + CHAR(10) + ''GO'' + CHAR(13) + CHAR(10) + ''IF EXISTS (SELECT

Error when trying to execute a command in sql server

痴心易碎 提交于 2020-04-20 18:37:29
问题 I am using SQL Server 2014 and working on creating views from all the user database on to a Test database. The generated sql statement works fine when run from SSMS but when the execute command tries to run it I get the following error message. SQL Query: DECLARE @SQL NVARCHAR(MAX), @QueryStmt NVARCHAR(MAX) SET @SQL = '' SELECT @SQL = @SQL + CHAR(13) + 'USE ' + QUOTENAME([name]) + '; SELECT @QueryStmt = ''USE TestDWH ''+ CHAR(13) + CHAR(10) + ''GO'' + CHAR(13) + CHAR(10) + ''IF EXISTS (SELECT

Error when trying to execute a command in sql server

▼魔方 西西 提交于 2020-04-20 18:36:54
问题 I am using SQL Server 2014 and working on creating views from all the user database on to a Test database. The generated sql statement works fine when run from SSMS but when the execute command tries to run it I get the following error message. SQL Query: DECLARE @SQL NVARCHAR(MAX), @QueryStmt NVARCHAR(MAX) SET @SQL = '' SELECT @SQL = @SQL + CHAR(13) + 'USE ' + QUOTENAME([name]) + '; SELECT @QueryStmt = ''USE TestDWH ''+ CHAR(13) + CHAR(10) + ''GO'' + CHAR(13) + CHAR(10) + ''IF EXISTS (SELECT

COPY with dynamic file name

老子叫甜甜 提交于 2020-04-18 05:46:00
问题 I am trying to write a function to load csv data into a table. I want the input argument to be the path to the file. CREATE OR REPLACE FUNCTION public.loaddata(filepathname varchar) RETURNS void AS $BODY$ BEGIN COPY climatedata( climatestationid, date, prcp, prcpqflag, prcpmflag, prcpsflag, tmax, tmaxqflag, tmaxmflag, tmaxsflag, tmin, tminqflag, tminmflag, tminsflag) FROM $1 WITH csv header; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.filltmaxa(character varying)

Repeating the same bind variable multiple times when using the OPEN…FOR dynamic SQL structure in Oracle PL/SQL

試著忘記壹切 提交于 2020-03-22 09:03:31
问题 This is a follow on question to Vincent Malgrat's answer to this question. I can't find the correct syntax to use when you need to use the same bind variable multiple times when using OPEN...FOR dynamic SQL. You can see the syntax for EXECUTE IMMEDIATE here (see "Using Duplicate Placeholders with Dynamic SQL") … but not for OPEN...FOR . Does the syntax differ with duplicate placeholders when using OPEN...FOR ? I'm using Oracle 12c. This is in a PL/SQL package not an anonymous block. For

Dynamic table design (common lookup table), need a nice query to get the values

和自甴很熟 提交于 2020-02-25 04:18:11
问题 sql2005 This is my simplified example: (in reality there are 40+ tables in here, I only showed 2) I got a table called tb_modules, with 3 columns (id, description, tablename as varchar): 1, UserType, tb_usertype 2, Religion, tb_religion (Last column is actually the name of a different table) I got an other table that looks like this: tb_value (columns:id, tb_modules_ID, usertype_OR_religion_ID) values: 1111, 1, 45 1112, 1, 55 1113, 2, 123 1114, 2, 234 so, I mean 45, 55, 123, 234 are usertype

How to use dynamic column names in an UPDATE or SELECT statement in a function?

感情迁移 提交于 2020-02-25 03:38:06
问题 In PostgreSQL 9.1, PL/pgSQL, given a query: select fk_list.relname from ... where relname is of type name (e.g., "table_name"). How do you get the appropriate value for "relname" that can be used directly in an UPDATE statement as: Update <relname> set ... within the PL/pgSQL script? Using quote_ident(r.relname) as: Update quote_ident(r.relname) Set ... fails with: syntax error at or near "(" LINE 55: UPDATE quote_ident(r.relname) .... The complete code I am working with: CREATE FUNCTION

How to return a table by rowtype in PL/pgSQL

安稳与你 提交于 2020-02-25 01:28:06
问题 I am trying to implement a function that returns a table with the same structure as an input table in the parameter, using PL/pgSQL (PostgreSQL 9.3). Basically, I want to update a table, and return a copy of the updated table with plpgsql. I searched around SO and found several related questions (e.g. Return dynamic table with unknown columns from PL/pgSQL function and Table name as a PostgreSQL function parameter), which lead to the following minimal test example: CREATE OR REPLACE FUNCTION

How do I use dynamic SQL to declare a column name derived from a table name?

ぐ巨炮叔叔 提交于 2020-02-08 09:37:09
问题 Building on Tony's answer on this question: If I want to do something like this, CREATE PROCEDURE A(tab IN VARCHAR2) IS tab.col_name <column> --static declaration (column name always remains the same) BEGIN EXECUTE IMMEDIATE 'INSERT INTO ' || tab(col_name) || 'VALUES(123)'; END A; How can I use Dynamic SQL in the above case? 回答1: This example passes in a table name and a column name: CREATE PROCEDURE A ( tab IN VARCHAR2 , col_name IN VARCHAR2 ) IS BEGIN EXECUTE IMMEDIATE 'INSERT INTO ' || tab

How do I use dynamic SQL to declare a column name derived from a table name?

风格不统一 提交于 2020-02-08 09:35:10
问题 Building on Tony's answer on this question: If I want to do something like this, CREATE PROCEDURE A(tab IN VARCHAR2) IS tab.col_name <column> --static declaration (column name always remains the same) BEGIN EXECUTE IMMEDIATE 'INSERT INTO ' || tab(col_name) || 'VALUES(123)'; END A; How can I use Dynamic SQL in the above case? 回答1: This example passes in a table name and a column name: CREATE PROCEDURE A ( tab IN VARCHAR2 , col_name IN VARCHAR2 ) IS BEGIN EXECUTE IMMEDIATE 'INSERT INTO ' || tab