dynamic-sql

How to execute a string result of a stored procedure in postgres

白昼怎懂夜的黑 提交于 2019-11-29 15:38:07
I have created the following stored procedure, which basically receives a name of table, and a prefix. The function then finds all columns that share this prefix and returns as an output a 'select' query command ('myoneliner'). as follows: CREATE OR REPLACE FUNCTION mytext (mytable text, myprefix text) RETURNS text AS $myoneliner$ declare myoneliner text; BEGIN SELECT 'SELECT ' || substr(cols,2,length(cols)-2) ||' FROM '||mytable INTO myoneliner FROM ( SELECT array( SELECT DISTINCT quote_ident(column_name::text) FROM information_schema.columns WHERE table_name = mytable AND column_name LIKE

Query the schema details of a table in PostgreSQL?

若如初见. 提交于 2019-11-29 14:20:59
问题 I need to know the column type in PostgreSQL (i.e. varchar(20) ). I know that I could probably find this using \d something in psql, but I need it to be done with a select query. Is this possible in PostgreSQL? 回答1: You can fully describe a table using postgres with the following query: SELECT a.attname as Column, pg_catalog.format_type(a.atttypid, a.atttypmod) as Datatype FROM pg_catalog.pg_attribute a WHERE a.attnum > 0 AND NOT a.attisdropped AND a.attrelid = ( SELECT c.oid FROM pg_catalog

Select columns with particular column names in PostgreSQL

送分小仙女□ 提交于 2019-11-29 13:55:38
I want to write a simple query to select a number of columns in PostgreSQL. However, I keep getting errors - I tried a few options but they did not work for me. At the moment I am getting the following error: org.postgresql.util.PSQLException: ERROR: syntax error at or near "column" To get the columns with values I try the followig: select * from weather_data where column like '%2010%' Any ideas? column is a reserved word . You cannot use it as identifier unless you double-quote it. Like: "column" . Doesn't mean you should, though. Just don't use reserved words as identifiers. Ever. To ...

Dynamic SQL error converting nvarchar to int

北慕城南 提交于 2019-11-29 13:31:07
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 WHERE ID = @DataID SET @SQL = 'SELECT ' + @GName + ' AS GrName ,' + @BR + @T_ID + ' AS To_ID ,' + @BR +

T-SQL looping through XML data column to derive unique set of paths

痞子三分冷 提交于 2019-11-29 12:56:17
I have XML data column which contains question and answer as part of an application process. What I am trying to achieve through T-SQL/ Dynamic SQL is to derive a unique set of path wherever there is a target tag. So for the below xml example, I would expect something like log/clients/client/section/questions/groupone/question/target log/clients/client/section/questions/grouptwo/question/target Idea is to then to use this and loop through the XML to derive the values of the desired tags. I.e [DATA].value('(/log/clients/client/section/questions/groupone/question/target', 'NVARCHAR(MAX)')

Creating a trigger for child table insertion returns confusing error

本秂侑毒 提交于 2019-11-29 12:19:05
I am trying to write a trigger function that will input values into separate child tables, however I am getting an error I have not seen before. Here is an example set up: -- create initial table CREATE TABLE public.testlog( id serial not null, col1 integer, col2 integer, col3 integer, name text ); -- create child table CREATE TABLE public.testlog_a (primary key(id)) INHERITS(public.testlog); -- make trigger function for insert CREATE OR REPLACE FUNCTION public.test_log() RETURNS trigger AS $$ DECLARE qry text; BEGIN qry := 'INSERT INTO public.testlog_' || NEW.name || ' SELECT ($1).*'; EXECUTE

Set empty strings ('') to NULL in the whole database

北城以北 提交于 2019-11-29 08:49:22
In my database are many text columns where values are empty strings ( '' ). The empty strings need to be set to NULL . I do not know the exact schemas, tables and columns in this database or rather I want to write a general solution which can be reused. How would I write a query / function to find all text columns in all tables in all schemas and update all columns with empty strings ( '' ) to NULL ? Erwin Brandstetter The most efficient way to achieve this: Run a single UPDATE per table. Only update nullable columns (not defined NOT NULL ) with any actual empty string. Only update rows with

How to dynamically order by certain entity properties in Entity Framework 7 (Core)

烂漫一生 提交于 2019-11-29 07:35:22
I have a project where the front-end JavaScript specifies a list of columns to order by. Then in the back-end I have multi-layer application. Typical scenario Service layer (the service models' (DTO) properties match whatever the client-side wants to order by) Domain layer (it exposes repository interfaces to access persisted objects) ORM layer (it implements the repository and it uses Entity Framework 7 (a.k.a Entity Framework Core) to access a SQL Server database) Please note that System.Linq.Dynamic IS NOT supported for DNX Core v5.0 or .NET Platform v5.4 so I cannot use that library. I

dynamic sql error: 'CREATE TRIGGER' must be the first statement in a query batch

北慕城南 提交于 2019-11-29 06:51:36
As part of some administrative tasks, we have many tables that each need a trigger created. The trigger will set a flag and the date in the Audit database when an object has been modified. For simplicity, I have a table with all the objects that need triggers created. I am trying to generate some dynamic sql to do this for each object, but I am getting this error: 'CREATE TRIGGER' must be the first statement in a query batch. Here is the code to generate the sql. CREATE PROCEDURE [spCreateTableTriggers] AS BEGIN DECLARE @dbname varchar(50), @schemaname varchar(50), @objname varchar(150),

Return dynamic table with unknown columns from PL/pgSQL function

和自甴很熟 提交于 2019-11-29 03:40:59
I need to create a function that checks on a given table if the infowindow field exists. If it exists the function must return select * from table but if it does not, it must return an additional id field: CREATE OR REPLACE FUNCTION getxo_ocx_cincu_preparar_infowindow( guretabla character varying) RETURNS TABLE AS $BODY$ DECLARE tabla ALIAS FOR $1; BEGIN IF EXISTS (SELECT 1 FROM pg_namespace n JOIN pg_class c ON c.relnamespace = n.oid JOIN pg_attribute a ON a.attrelid = c.oid WHERE n.nspname = current_schema() -- default to current schema AND c.relname = tabla AND a.attname = 'infowindow' AND