dynamic-sql

Error in Dynamic SQL

别说谁变了你拦得住时间么 提交于 2020-02-07 02:32:52
问题 I Have Created the following SP to generate the Update Statements and it uses the table Col.TMap.T_Mp and the data in the table looks like: ID M_Type ID_F SF1 SF2 1 Acc ACC_ID AC_ID NULL 1 STA STA_ID ST_ID NULL 1 CHa Cha_ID CH_ID NULL CREATE PROCEDURE dbo.dtmap( @ID INT ) AS BEGIN DECLARE @SQL NVARCHAR(MAX) DECLARE @SchemaName SYSNAME DECLARE @TableName SYSNAME DECLARE @DatabaseName SYSNAME DECLARE @M_Type SYSNAME DECLARE @SF1 VARCHAR(50) DECLARE @SF2 VARCHAR(50) DECLARE @ID_F VARCHAR(50)

How to use if exists- if not exists in PL/SQL?

混江龙づ霸主 提交于 2020-01-30 11:31:47
问题 I am trying to convert if exists statement from SQL-Server to PL/SQL but having an error. I am trying to check if NAME_1 doesn't exist in my table_1 , if they don't exist then I am checking if COLUMN_NAME='NAME_2' exist in my table_1 , if it exist then insert (NAME_1 and NAME_2) into my table_2 . Thanks T-SQL (SQL-Server): IF NOT (EXISTS (SELECT * from table_name_1 where name='NAME_1')) BEGIN IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table_1' AND COLUMN_NAME=

Access dynamic column name of row type in trigger function

巧了我就是萌 提交于 2020-01-24 21:17:27
问题 I am trying to create a dynamic function to use for setting up triggers. CREATE OR REPLACE FUNCTION device_bid_modifiers_count_per() RETURNS TRIGGER AS $$ DECLARE devices_count INTEGER; table_name regclass := TG_ARGV[0]; column_name VARCHAR := TG_ARGV[1]; BEGIN LOCK TABLE device_types IN EXCLUSIVE MODE; EXECUTE format('LOCK TABLE %s IN EXCLUSIVE MODE', table_name); SELECT INTO devices_count device_types_count(); IF TG_OP = 'DELETE' THEN SELECT format( 'PERFORM validate_bid_modifiers_count(%s,

Understanding difference between int literal vs int parameter in PL/pgSQL function

妖精的绣舞 提交于 2020-01-24 20:42:23
问题 I have a function to left pad bit stings in PostgreSQL 9.5: CREATE OR REPLACE FUNCTION lpad_bits(val bit varying) RETURNS bit varying as $BODY$ BEGIN return val::bit(32) >> (32-length(val)); END; $BODY$ LANGUAGE plpgsql IMMUTABLE; which works fine: # select lpad_bits(b'1001100111000'); lpad_bits ---------------------------------- 00000000000000000001001100111000 (1 row) My problem is when I try to add a parameter to change the amount of padding: CREATE OR REPLACE FUNCTION lpad_bits(val bit

Oracle - using bind variable in LIKE clause of dynamic cursor

老子叫甜甜 提交于 2020-01-21 04:57:05
问题 I am using dynamic cursor for fetching data. Query that is being executed looks similar to: query := 'SELECT column1, column2 FROM my_table WHERE column1 LIKE ''%:bv1%'''; And the cursor itself is executed like this: OPEN my_cursor FOR query USING my_var1; I also tried to check the query and print it: ... WHERE column1 LIKE '%:bv1%' ... so apostrophes are escaped, but the cursor fetches no data. Is it even possible to use bind variables in LIKE clause and if yes, what did I do wrong? 回答1:

Dynamic SQL using config tables

放肆的年华 提交于 2020-01-17 05:29:12
问题 I have a table which contains a list of dynamic SQL views that needs to be created SEEDING_TABLE ------------- KEYVALUE|VIEW_TO_BE_CREATED|FROMTABLE|NOOFCOLS 1|A|A1|3 2|B|B1|4 3|C|C1|5 The other table which contains the actual column names for the above seeding table ORDERCOLS_FORVIEW KEYVALUE|FROMTABLE|COLSAVAILABLE 1|A1|NUM1 1|A1|NUM2 1|A1|NUM3 2|B1|NUM1 2|B1|NUM2 2|B1|NUM3 2|B1|NUM4 3|C1|NUM1 3|C1|NUM2 3|C1|NUM3 3|C1|NUM4 3|C1|NUM5 Definition of the table FROMTABLEs as follows A1 ->

SQL/Dynamic SQL - Add column if it does not exist

时光怂恿深爱的人放手 提交于 2020-01-15 11:22:30
问题 I have the SQL query below: create table #temp ( Account varchar(5), category varchar(6), amount money ) insert into #temp values ('A001', 'ABC-CO', 1000.00) insert into #temp values ('A002', 'DEF-CR', 500.00) insert into #temp values ('A002', 'GHI-PR', 800.00) insert into #temp values ('A003', 'DEF', 700.00) insert into #temp values ('A004', 'ABC-PR', 1100.00) DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX); SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(REPLACE(REPLACE(REPLACE(c

Update column in multiple tables

风格不统一 提交于 2020-01-15 05:36:09
问题 Let's say I have a column called partner in multiple tables within one schema: select table_name from information_schema.columns where column_name = 'partner'; How would I update all columns where value partner = 100 to partner = 101 ? 回答1: For a one-time operation, a DO statement executing dynamic SQL should serve just fine: DO $do$ DECLARE _tbl text; BEGIN FOR _tbl IN SELECT quote_ident(table_name) -- escape identifier! FROM information_schema.columns WHERE table_schema = 'public' -- your

How to select from variable that is a table name n Postgre >=9.2

痞子三分冷 提交于 2020-01-14 13:14:11
问题 i have a variable that is a name of a table. How can i select or update from this using variable in query , for example: create or replace function pg_temp.testtst () returns varchar(255) as $$ declare r record; t_name name; begin for r in SELECT tablename FROM pg_tables WHERE schemaname = 'public' limit 100 loop t_name = r.tablename; update t_name set id = 10 where id = 15; end loop; return seq_name; end; $$ language plpgsql; it shows ERROR: relation "t_name" does not exist 回答1: Correct

How to select from variable that is a table name n Postgre >=9.2

拟墨画扇 提交于 2020-01-14 13:14:05
问题 i have a variable that is a name of a table. How can i select or update from this using variable in query , for example: create or replace function pg_temp.testtst () returns varchar(255) as $$ declare r record; t_name name; begin for r in SELECT tablename FROM pg_tables WHERE schemaname = 'public' limit 100 loop t_name = r.tablename; update t_name set id = 10 where id = 15; end loop; return seq_name; end; $$ language plpgsql; it shows ERROR: relation "t_name" does not exist 回答1: Correct