dynamic-sql

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

两盒软妹~` 提交于 2019-12-18 20:48:07
问题 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

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

╄→尐↘猪︶ㄣ 提交于 2019-12-18 16:52:30
问题 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) 回答1: 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

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

百般思念 提交于 2019-12-18 11:56:02
问题 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

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

本小妞迷上赌 提交于 2019-12-18 10:08:20
问题 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

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

不打扰是莪最后的温柔 提交于 2019-12-18 09:17:15
问题 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

Select columns with particular column names in PostgreSQL

寵の児 提交于 2019-12-18 08:31:35
问题 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? 回答1: column is a reserved word. You cannot use it as identifier unless you double-quote it. Like:

Creating a trigger for child table insertion returns confusing error

[亡魂溺海] 提交于 2019-12-18 07:06:40
问题 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 $

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

邮差的信 提交于 2019-12-18 05:20:33
问题 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 ? 回答1: The most efficient way to achieve this: Run a single UPDATE per table. Only update

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

╄→гoц情女王★ 提交于 2019-12-18 04:51:01
问题 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

Binding Parameters to Oracle Dynamic SQL

Deadly 提交于 2019-12-17 22:43:23
问题 I have a stored procedure that accepts multiple parameters (i.e. pName, pHeight, pTeam) I have the query built up like this: SQLQuery VARCHAR2(6000); TestCursor T_CURSOR; SQLQuery := 'SELECT ID, Name, Height, Team FROM MyTable WHERE ID IS NOT NULL '; -- Build the query based on the parameters passed. IF pName IS NOT NULL SQLQuery := SQLQuery || 'AND Name LIKE :pName '; END IF; IF pHeight IS > 0 SQLQuery := SQLQuery || 'AND Height = :pHeight '; END IF; IF pTeam IS NOT NULL SQLQuery := SQLQuery