dynamic-sql

PostgreSQL - Writing dynamic sql in stored procedure that returns a result set

你。 提交于 2019-11-30 05:14:07
How can I write a stored procedure that contains a dynamically built SQL statement that returns a result set? Here is my sample code: CREATE OR REPLACE FUNCTION reporting.report_get_countries_new ( starts_with varchar, ends_with varchar ) RETURNS TABLE ( country_id integer, country_name varchar ) AS $body$ DECLARE starts_with ALIAS FOR $1; ends_with ALIAS FOR $2; sql VARCHAR; BEGIN sql = 'SELECT * FROM lookups.countries WHERE lookups.countries.country_name >= ' || starts_with ; IF ends_with IS NOT NULL THEN sql = sql || ' AND lookups.countries.country_name <= ' || ends_with ; END IF; RETURN

Dynamically generate columns in PostgreSQL

坚强是说给别人听的谎言 提交于 2019-11-30 04:04:53
问题 I have seen that there are quit a few similar questions like this one, but I havent understood how to code it myself. Please have in mind that I am just a beginner in this field. Basically I want to pivot the table like this: zoom | day | point zoom | 2015-10-01 | 2015-10-02 | ...... ------+-----------+------- ---> ------+------------+-------------+ 1 | 2015-10-01 | 201 1 | 201 | 685 | 2 | 2015-10-01 | 43 2 | 43 | 346 | 3 | 2015-10-01 | 80 3 | 80 | 534 | 4 | 2015-10-01 | 324 4 | 324 | 786 | 5

How do I execute a very long dynamic sql statement?

走远了吗. 提交于 2019-11-29 22:31:38
问题 I remember back in the day I would make a whole wack of nvarchar(4000) vars, check the length of them as they grew, switch them out as they filled up and then concatenate the whole mess together for the exec call. I was wondering if there was an easier way of doing it. Thanks! Edit: Code Sample, shows me screwing up the case statement DECLARE @sql NVARCHAR(MAX) SELECT @sql = CAST(N'SELECT ' AS NVARCHAR(MAX)) DECLARE @Index INT SELECT @Index = 0 WHILE (@Index < 1000) BEGIN SELECT @sql = CAST(

Create PostgreSQL ROLE (user) if it doesn't exist

情到浓时终转凉″ 提交于 2019-11-29 20:32:08
How do I write an SQL script to create a ROLE in PostgreSQL 9.1, but without raising an error if it already exists? The current script simply has: CREATE ROLE my_user LOGIN PASSWORD 'my_password'; This fails if the user already exists. I'd like something like: IF NOT EXISTS (SELECT * FROM pg_user WHERE username = 'my_user') BEGIN CREATE ROLE my_user LOGIN PASSWORD 'my_password'; END; ... but that doesn't work - IF doesn't seem to be supported in plain SQL. I have a batch file that creates a PostgreSQL 9.1 database, role and a few other things. It calls psql.exe, passing in the name of an SQL

How to dynamically build an insert command from Datatable in c#

a 夏天 提交于 2019-11-29 19:05:41
问题 I am facing some problem with making a SQL insert statement dynamically from a dataTable object in c#. I want to know the best practices to make it.Here is my code snippet , I have tried so far. String sqlCommandInsert = "INSERT INTO dbo.RAW_DATA("; String sqlCommandValue = ""; foreach (DataColumn dataColumn in dataTable.Columns) { sqlCommandInsert += dataColumn + ","; } sqlCommandInsert += sqlCommandInsert.TrimEnd(','); sqlCommandInsert += ") VALUE("; for (int i = 0; i < dataTable.Rows.Count

dynamic table name in select statement

十年热恋 提交于 2019-11-29 17:04:12
I have a series of history tables in an oracle 9 database. History_table_00 contains last months data, History_table_01 contains the month before, and History_table_02 the month before that. Next month, History_table_02 will automatically get renamed to history_table_03, history_table_01 renamed to history_table_02, history_table_00 renamed to history_table_01, and a new history_table_00 will be created to gather the newest history (I really hope I am making sense). Anyway, I need to write a select statement that will dynamically select all history tables. I am hoping this won't be too

drop all tables sharing the same prefix in postgres

只谈情不闲聊 提交于 2019-11-29 16:54:16
问题 I would like to delete all tables sharing the same prefix ('supenh_agk') from the same database, using one sql command/query. 回答1: To do this in one command you need dynamic SQL with EXECUTE in a DO statement (or function): DO $do$ DECLARE _tbl text; BEGIN FOR _tbl IN SELECT quote_ident(table_schema) || '.' || quote_ident(table_name) -- escape identifier and schema-qualify! FROM information_schema.tables WHERE table_name LIKE 'prefix' || '%' -- your table name prefix AND table_schema NOT LIKE

Elegant way of handling PostgreSQL exceptions?

天大地大妈咪最大 提交于 2019-11-29 16:52:38
问题 In PostgreSQL, I would like to create a safe-wrapping mechanism which returns empty result if an exception occurs. Consider the following: SELECT * FROM myschema.mytable; I could do the safe-wrapping in the client application: try { result = execute_query('SELECT value FROM myschema.mytable').fetchall(); } catch(pg_exception) { result = [] } But could I do such a thing in SQL directly? I would like to make the following code work, but it seems like it should by put into DO $$ ... $$ block and

Get values from varying columns in a generic trigger

前提是你 提交于 2019-11-29 16:34:40
I am new to PostgreSQL and found a trigger which serves my purpose completely except for one little thing. The trigger is quite generic and runs across different tables and logs different field changes. I found here . What I now need to do is test for a specific field which changes as the tables change on which the trigger fires. I thought of using substr as all the column will have the same name format e.g. XXX_cust_no but the XXX can change to 2 or 4 characters. I need to log the value in the XXX_cust_no field with every record that is written to the history_ / audit table. Using a bunch of

Simplify Dynamic SQL Pivot Table

我怕爱的太早我们不能终老 提交于 2019-11-29 16:30:18
I have written a Dynamic Pivot Table Query based on the following. Here is a SQL FIDDLE for reference. CREATE TABLE TestTable1 ([idnumber] INT, [DataTypeId] INT) GO INSERT INTO TestTable1 VALUES (1, 108), (1, 108), (1, 108), (2, 108), (2, 108), (3, 108), (1, 109),(1, 109), (1, 110),(2, 110),(1, 111),(4, 108),(4, 108), (4, 110),(4, 111) GO Here is the Dynamic SQL that I wrote DECLARE @SQL NVARCHAR(MAX), @Cols NVARCHAR(MAX), @ColsP NVARCHAR(MAX) SELECT @Cols = STUFF((select ', ISNULL([' + CAST([DataTypeId] as varchar(10)) + '], 0) AS ''' + CAST([DataTypeId] as varchar(10)) + '''' FROM ( SELECT