dynamic-sql

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

穿精又带淫゛_ 提交于 2020-01-14 13:12:04
问题 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

TSQL Passing MultiValued Reporting Services Parameter into Dynamic SQL

半世苍凉 提交于 2020-01-14 06:49:09
问题 Duplicate of : TSQL varchar string manipulation I'm building a dynamic SQL statement out of parameters from a reporting services report. Reporting services passes MutiValue Parameters in a basic CSV format. For example a list of states may be represented as follows: AL,CA,NY,TN,VA In a SQL statement this is OK: WHERE customerState In (@StateList) However, the dynamic variant isn't OK: SET @WhereClause1 = @WhereClause1 + 'AND customerState IN (' + @StateList + ') ' This is because it

Is there a way to select a database from a variable?

蹲街弑〆低调 提交于 2020-01-13 11:45:09
问题 Is there a way to select a database from a variable? Declare @bob as varchar(50); Set @bob = 'SweetDB'; GO USE @bob 回答1: Unfortunately, no. Unless you can execute the rest of your batch as dynamic SQL. Using execute to dynamically execute SQL will change the context for the scope of the execute statement, but will not leave a lasting effect on the scope you execute the execute statement from. In other words, this: DECLARE @db VARCHAR(100) SET @db = 'SweetDB' EXECUTE('use ' + @db) Will not set

Create dynamic select get value for column name - in SQL Server

旧城冷巷雨未停 提交于 2020-01-13 11:28:32
问题 Please help me create a select SQL statement with the results column name get from the column values in origin table (tablename is Device_Part ): User can input many DeviceCode which have many dynamic PartTypeName , the PartTypeName value is the PartInfo . 回答1: This may help: CREATE Table Device ( DeviceCode NVARCHAR(100) NOT NULL, PartTypeName NVARCHAR(100) NOT NULL, PartInfo NVARCHAR(100) NOT NULL ) Insert Into Device Values('VT.SX-01','CPU','Pentium G6650'), ('VT.SX-01','Motherboard','H81M

PostgreSQL drop constraint with unknown name

…衆ロ難τιáo~ 提交于 2020-01-11 05:11:45
问题 I have an SQL script that needs to drop several constraints and restore them at the end, but the constraint names are auto-generated and will be different each time the script is run. I know how to get the constraint name from the table names, but it doesn't seem possible to use this information in the drop statement. select conname from pg_constraint where conrelid = (select oid from pg_class where relname='table name') and confrelid = (select oid from pg_class where relname='reference table

Update record of a cursor where the table name is a parameter

我怕爱的太早我们不能终老 提交于 2020-01-11 02:36:19
问题 I am adjusting some PL/pgSQL code so my refcursor can take the table name as parameter. Therefore I changed the following line: declare pointCurs CURSOR FOR SELECT * from tableName for update; with this one: OPEN pointCurs FOR execute 'SELECT * FROM ' || quote_ident(tableName) for update; I adjusted the loop, and voilà, the loop went through. Now at some point in the loop I needed to update the record (pointed by the cursor) and I got stuck. How should I properly adjust the following line of

Use variable set by psql meta-command inside of DO block

£可爱£侵袭症+ 提交于 2020-01-10 05:11:21
问题 Here's what I would like to do: \set values foo,bar,baz DO $$ DECLARE value TEXT; values TEXT[] := string_to_array(:'values', ','); BEGIN FOREACH value IN ARRAY values LOOP raise notice 'v: %', value; END LOOP; END $$ LANGUAGE plpgsql; Which results in the following error: ERROR: syntax error at or near ":" SELECT string_to_array(:'values', ',') INTO values... ^ Here's the solution I have currently, but it feels hacky: \set values foo,bar,baz PREPARE get_values AS SELECT string_to_array(:

Custom SQL column formulas without dynamic SQL

雨燕双飞 提交于 2020-01-07 05:02:11
问题 We have a program that allows users to map raw unmodified input data to a standardized final table. In general it's a simple one-to-one match without any special logic needed. For example; raw_table.raw_col_1 will map to final_table.col_1, raw_table.raw_col_2 will map to final_table.col_2, etc. However, one customer wants the ability to have final_table.col_3 to be mapped as follows: case when (raw_col_1 = 'S12' and raw_col_2 = 'D18') or raw_col_3 is not null then raw_col_3 else 'GF17' end

How do you specify IN clause in a dynamic query using a variable?

丶灬走出姿态 提交于 2020-01-06 21:05:36
问题 In PL/SQL, you can specify the values for the IN operator using concatenation: v_sql := 'select field1 from table1 where field2 in (' || v_list || ')'; Is it possible to do the same using a variable? v_sql := 'select field1 from table1 where field2 in (:v_list)'; If so, how? EDIT: With reference to Marcin's answer, how do I select from the resultant table? declare cursor c_get_csv_as_tables is select in_list(food_list) food_list from emp_food where emp_type = 'PERM'; cursor c_get_food_list (v

Select columns with the same prefix [duplicate]

依然范特西╮ 提交于 2020-01-06 17:27:53
问题 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