dynamic-sql

PL/SQL rewrite concatenated query with 'IN' clause

淺唱寂寞╮ 提交于 2019-12-31 04:08:09
问题 Currently I have in my pl/sql code following statements: -- vList looks like '1,2,3,4' vStatement := 'SELECT NAME FROM T_USER WHERE ID IN ( ' || vList || ' ) '; Execute Immediate vStatement BULK COLLECT INTO tNames; I think that concatenating of query if bad practice, so I want to make this query without using stings. What is the way to rewrite this ? P.S. maybe people here can point out why concatenation of queries is bad, because i don't have enough reasons to prove that this style is bad.

What is the advantage of using @ParmDefinition in sp_executesql

余生颓废 提交于 2019-12-31 03:29:08
问题 DECLARE @id int DECLARE @name nvarchar(20) SET @id = 5 SET @name = 'Paul' What is the difference between these two options: Set @SQLQueryInnen = 'SELECT * FROM someTable WHERE ID = ' + @id + ' AND NAME = ''' + @name + '''' Execute sp_Executesql @SQLQueryInnen and Set @SQLQueryInnen = 'SELECT * FROM someTable WHERE ID = @id AND NAME = @name' Set @ParmDefinition = '@id int, @name nvarchar(20)' Execute sp_Executesql @SQLQueryInnen, @ParmDefinition, @id So far I only see the overhad for declaring

Simple dynamic TSQL query syntax

前提是你 提交于 2019-12-30 06:24:28
问题 This may be an easy answer but I've been staring at it for too long... I have the following query that takes a stored procedure input parameter as a variable name and counts the records in that table. I'd like to retrieve the results of the dynamic statement (@toStartStr) into a variable (@toStart). -- @tempTableName = SProc input parameter DECLARE @toStartStr nvarchar(150); DECLARE @toStart int; SET @toStartStr = 'SELECT @toStart = COUNT(ID) FROM ' + @tempTableName; EXEC(@toStartStr); Right

Dynamic SQL error converting nvarchar to int

╄→гoц情女王★ 提交于 2019-12-29 08:33:21
问题 I have created a procedure in dynamic SQL which has a select statement and the code looks like: ALTER PROCEDURE cagroup ( @DataID INT , @days INT , @GName VARCHAR(50) , @T_ID INT , @Act BIT , @Key VARBINARY(16) ) AS BEGIN DECLARE @SQL NVARCHAR(MAX) DECLARE @SchemaName SYSNAME DECLARE @TableName SYSNAME DECLARE @DatabaseName SYSNAME DECLARE @BR CHAR(2) SET @BR = CHAR(13) + CHAR(10) SELECT @SchemaName = Source_Schema , @TableName = Source_Table , @DatabaseName = Source_Database FROM Source

Execute a dynamic crosstab query

拜拜、爱过 提交于 2019-12-27 12:28:02
问题 I implemented this function in my Postgres database: http://www.cureffi.org/2013/03/19/automatically-creating-pivot-table-column-names-in-postgresql/ Here's the function: create or replace function xtab (tablename varchar, rowc varchar, colc varchar, cellc varchar, celldatatype varchar) returns varchar language plpgsql as $$ declare dynsql1 varchar; dynsql2 varchar; columnlist varchar; begin -- 1. retrieve list of column names. dynsql1 = 'select string_agg(distinct '||colc||'||'' '|

Execute a dynamic crosstab query

拈花ヽ惹草 提交于 2019-12-27 12:27:10
问题 I implemented this function in my Postgres database: http://www.cureffi.org/2013/03/19/automatically-creating-pivot-table-column-names-in-postgresql/ Here's the function: create or replace function xtab (tablename varchar, rowc varchar, colc varchar, cellc varchar, celldatatype varchar) returns varchar language plpgsql as $$ declare dynsql1 varchar; dynsql2 varchar; columnlist varchar; begin -- 1. retrieve list of column names. dynsql1 = 'select string_agg(distinct '||colc||'||'' '|

find column name or table name of a specific value

女生的网名这么多〃 提交于 2019-12-26 07:42:47
问题 I want search a specific value in my database that i don't know where is it exactly. Is there any query exist that returned column name or table name of a specific value in SQL server? Assume that I have a value of a column like 123, but I don't know 123 belongs to which table and I don't know any about its column name. Can i write a query to find table names that this value is in it? I need a query not a procedure!!! 回答1: This might do it for you. Note that if you have a lot of tables

find column name or table name of a specific value

末鹿安然 提交于 2019-12-26 07:40:54
问题 I want search a specific value in my database that i don't know where is it exactly. Is there any query exist that returned column name or table name of a specific value in SQL server? Assume that I have a value of a column like 123, but I don't know 123 belongs to which table and I don't know any about its column name. Can i write a query to find table names that this value is in it? I need a query not a procedure!!! 回答1: This might do it for you. Note that if you have a lot of tables

Postgres syntax error in a function

那年仲夏 提交于 2019-12-25 09:35:08
问题 I am trying to create a function and I can't find my error in the following code: CREATE OR REPLACE FUNCTION qwat_od.fn_label_create_fields(table_name varchar, position boolean = true, rotation boolean = true) RETURNS void AS $BODY$ BEGIN /* Creates columns */ EXECUTE 'ALTER TABLE qwat_od.'||table_name||' ADD COLUMN label_1_visible smallint default 1; '; IF position IS TRUE THEN EXECUTE 'ALTER TABLE qwat_od.'||table_name||' ADD COLUMN label_1_x double precision default null;'; EXECUTE 'ALTER

Converting a query to dynamic SQL with linked server variable

只愿长相守 提交于 2019-12-25 08:59:47
问题 I have a stored procedure that I need to convert so that it reads a linked server variable from a table. From what I know, the only way to do so is to use dynamic SQL. The problem is that I'm failing to convert my query. Original query: SET @var1 = '' SELECT @var1 = RECEIVER FROM databse1.dbo.table1 WHERE SAPNUMBER = @var2 Converted query: SET @srv = (SELECT server_name FROM Configuration.dbo.Server_Switch) SET @var1 = '' exec ( 'SELECT ' + @var1 + '= RECEIVER FROM ' + @srv + '.databse1.dbo