dynamic-sql

SQL update fields of one table from fields of another one

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 10:49:32
I have two tables: A [ID, column1, column2, column3] B [ID, column1, column2, column3, column4] A will always be subset of B (meaning all columns of A are also in B ). I want to update a record with a specific ID in B with their data from A for all columns of A . This ID exists both in A and B . Is there an UPDATE syntax or any other way to do that without specifying the column names, just saying "set all columns of A" ? I'm using PostgreSQL, so a specific non-standard command is also accepted (however, not preferred). You can use the non-standard FROM clause. UPDATE b SET column1 = a.column1,

Declare Variable for a Query String

孤人 提交于 2019-11-27 09:54:58
问题 I was wondering if there was a way to do this in MS SQL Server 2005: DECLARE @theDate varchar(60) SET @theDate = '''2010-01-01'' AND ''2010-08-31 23:59:59''' SELECT AdministratorCode, SUM(Total) as theTotal, SUM(WOD.Quantity) as theQty, AVG(Total) as avgTotal, (SELECT SUM(tblWOD.Amount) FROM tblWOD JOIN tblWO on tblWOD.OrderID = tblWO.ID WHERE tblWO.Approved = '1' AND tblWO.AdministratorCode = tblWO.AdministratorCode AND tblWO.OrderDate BETWEEN @theDate ) ... etc Is this possible to do? 回答1:

How to select column names dynamically in mySQL

半世苍凉 提交于 2019-11-27 09:45:43
I want to select column names but I don't know the table structure ahead of time and it may change so I can't just hard code the select statement with column names. I also do NOT want to select every column. Is there and easy way to do this? My thoughts are it's some kind of combination of these two queries but my SQL is not that good. SHOW COLUMNS FROM table_name; SELECT * FROM table_name; I tried using a sub select but it didn't work. Nothing seems to happen, I don't get an error I just get no results SELECT (SELECT column_name FROM information_schema.columns WHERE table_name ='table_name')

How to cleanse dynamic SQL in SQL Server — prevent SQL injection

别等时光非礼了梦想. 提交于 2019-11-27 09:16:34
We have a ton of SQL Server stored procedures which rely on dynamic SQL. The parameters to the stored procedure are used in a dynamic SQL statement. We need a standard validation function inside these stored procedures to validate these parameters and prevent SQL injection. Assume we have these constraints: We can't rewrite the procedures to not use Dynamic SQL We can't use sp_OACreate etc., to use regular expressions for validation. We can't modify the application which calls the stored procedure to validate the parameters before they are passed to the stored procedure. Is there a set of

T-SQL Dynamic SQL and Temp Tables

岁酱吖の 提交于 2019-11-27 08:54:10
It looks like #temptables created using dynamic SQL via the EXECUTE string method have a different scope and can't be referenced by "fixed" SQLs in the same stored procedure. However, I can reference a temp table created by a dynamic SQL statement in a subsequence dynamic SQL but it seems that a stored procedure does not return a query result to a calling client unless the SQL is fixed. A simple 2 table scenario: I have 2 tables. Let's call them Orders and Items. Order has a Primary key of OrderId and Items has a Primary Key of ItemId. Items.OrderId is the foreign key to identify the parent

COPY with dynamic file name

本小妞迷上赌 提交于 2019-11-27 07:44:20
问题 I am trying to write a function to load csv data into a table. I want the input argument to be the path to the file. CREATE OR REPLACE FUNCTION public.loaddata(filepathname varchar) RETURNS void AS $BODY$ BEGIN COPY climatedata( climatestationid, date, prcp, prcpqflag, prcpmflag, prcpsflag, tmax, tmaxqflag, tmaxmflag, tmaxsflag, tmin, tminqflag, tminmflag, tminsflag) FROM $1 WITH csv header; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.filltmaxa(character varying)

PL/pgSQL: General Way to Update N Columns in Trigger?

老子叫甜甜 提交于 2019-11-27 07:10:02
问题 I am attempting create a function that will take a general table and convert N columns to upper case. I haven't had any luck finding a solution to this type of problem, but I was able to come up with the following: create or replace function uc_on_insert() returns trigger as $$ declare p_tbl varchar = TG_TABLE_NAME; p_sch varchar = TG_TABLE_SCHEMA; i varchar; begin for i in (select column_name from INFORMATION_SCHEMA.COLUMNS where 1=1 and table_name ilike p_tbl and table_schema ilike p_sch

DROP FUNCTION without knowing the number/type of parameters?

柔情痞子 提交于 2019-11-27 06:52:57
I keep all my functions in a text file with 'CREATE OR REPLACE FUNCTION somefunction' . So if I add or change some function I just feed the file to psql. Now if I add or remove parameters to an existing function, it creates an overload with the same name and to delete the original I need type in all the parameter types in the exact order which is kind of tedious. Is there some kind of wildcard I can use to DROP all functions with a given name so I can just add DROP FUNCTION lines to the top of my file? You would need to write a function that took the function name, and looked up each overload

Function to return dynamic set of columns for given table

风格不统一 提交于 2019-11-27 06:19:08
问题 I have a fields table to store column information for other tables: CREATE TABLE public.fields ( schema_name varchar(100), table_name varchar(100), column_text varchar(100), column_name varchar(100), column_type varchar(100) default 'varchar(100)', column_visible boolean ); And I'd like to create a function to fetch data for a specific table. Just tried sth like this: create or replace function public.get_table(schema_name text, table_name text, active boolean default true) returns setof

Sanitize table/column name in Dynamic SQL in .NET? (Prevent SQL injection attacks)

萝らか妹 提交于 2019-11-27 05:26:00
I am generating some Dynamic SQL and would like to ensure that my code is safe from SQL injection . For sake of argument here is a minimal example of how it is generated: var sql = string.Format("INSERT INTO {0} ({1}) VALUES (@value)", tableName, columnName); In the above, tableName , columnName , and whatever is bound to @value come from an untrusted source. Since placeholders are being used @value is safe from SQL injection attacks, and can be ignored. (The command is executed via SqlCommand.) However, tableName and columnName cannot be bound as placeholders and are therefor vulnerable to