dynamic-sql

Prepend table name to each column in a result set in SQL? (Postgres specifically)

爷,独闯天下 提交于 2019-11-30 19:28:01
How can I get the label of each column in a result set to prepend the name if its table? I want this to happen for queries on single tables as well as joins. Example: SELECT first_name, last_name FROM person; I want the results to be: | person.first_name | person.last_name | |-------------------|------------------| | Wendy | Melvoin | | Lisa | Coleman | I could use "AS" to define an alias for each column, but that would be tedious. I want this to happen automatically. SELECT first_name AS person.first_name, last_name AS person.last_name FROM person; The reason for my question is that I am

SQL state: 42601 syntax error at or near “11”

若如初见. 提交于 2019-11-30 16:15:44
问题 I have a table address_all and it is inherited by several address tables. address_history inherits from parent table history_all and keeps current address information. I am creating new table which inherits address_all table and copies information from address_history to new table. My stored procedure is like this below. I am having some error when I call it. To better explain error I am using line number. 1 CREATE OR REPLACE FUNCTION somefunc() 2 RETURNS void AS 3 $BODY$ 4 DECLARE 5 year_id

EXECUTE…INTO…USING statement in PL/pgSQL can't execute into a record?

橙三吉。 提交于 2019-11-30 15:31:49
I'm attempting to write an area of a function in PL/pgSQL that loops through an hstore and sets a record's column(the key of the hstore ) to a specific value (the value of the hstore ). I'm using Postgres 9.1. The hstore will look like: ' "column1"=>"value1","column2"=>"value2" ' Generally, here is what I want from a function that takes in an hstore and has a record with values to modify: FOR my_key, my_value IN SELECT key, value FROM EACH( in_hstore ) LOOP EXECUTE 'SELECT $1' INTO my_row.my_key USING my_value; END LOOP; The error which I am getting with this code: "myrow" has no field "my_key

What is the SQL Server equivalent of Oracle bind variables in dynamic SQL?

有些话、适合烂在心里 提交于 2019-11-30 14:03:49
In Oracle, when writing dynamic SQL one does something like this: create or replace procedure myProc(n in number) as begin execute immediate 'update myTable set myColumn = :n' using n; commit; end; And then 'magic happens'. What, if any, is the equivalent concept / syntax in SQL Server? (BTW I'm using SQL Server 2005) You would use sp_executesql . The bound variables look like this: @var1 . From the below link, an example query against the standard Northwind database: DECLARE @IntVariable int; DECLARE @SQLString nvarchar(500); DECLARE @ParmDefinition nvarchar(500); /* Build the SQL string one

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

南笙酒味 提交于 2019-11-30 13:57:33
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; i++) { sqlCommandValue += "'" + dataTable.Rows[i].ItemArray[i] + "',"; } var insertCommand =

drop all tables sharing the same prefix in postgres

微笑、不失礼 提交于 2019-11-30 11:48:47
I would like to delete all tables sharing the same prefix ('supenh_agk') from the same database, using one sql command/query. Erwin Brandstetter 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 'pg_%' -- exclude system schemas LOOP RAISE NOTICE '%', -- EXECUTE 'DROP TABLE ' || _tbl

How to write a mysql function with dynamic table name?

我是研究僧i 提交于 2019-11-30 09:52:48
问题 I'm trying to write a my sql function doing the following things: 1- get the table name used in join as a parameter. but I get mysql syntax error 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table DETERMINISTIC BEGIN select `r`.`id` AS `id`, (case ' at line 2 This is my query DELIMITER $$ CREATE FUNCTION getTranslation (tablename varchar(50),entity varchar(20),itemid int,lang char(3)) RETURNS

Query the schema details of a table in PostgreSQL?

大兔子大兔子 提交于 2019-11-30 09:36:51
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? Alexandre GUIDET 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.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~

Get values from varying columns in a generic trigger

做~自己de王妃 提交于 2019-11-30 09:34:17
问题 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

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

萝らか妹 提交于 2019-11-30 08:45:18
问题 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