dynamic-sql

Passing column names dynamically for a record variable in PostgreSQL

徘徊边缘 提交于 2019-11-27 21:18:41
Using PostgreSQL, column values from a table for 1st record are stored in a record variable. for ex: let the variable be: recordvar recordvar.columnname gives the value of the column name specified. I will define the columname in a variable: var := columnname In place of columnname if I replace with the variable i.e. recordvar.var , it is not working. Please let me know how to proceed in this situation. Following is the sample code: CREATE OR REPLACE FUNCTION getrowdata(id numeric, table_name character varying) RETURNS SETOF void AS $BODY$ DECLARE srowdata record; reqfield character varying;

Function to loop through and select data from multiple tables

◇◆丶佛笑我妖孽 提交于 2019-11-27 20:54:23
I'm new to Postgres and have a database with multiple tables of the same structure. I need to select data from each table that matches certain criteria. I could do this with a bunch of UNION queries, but the number of tables I need to search can change over time, so I don't want to hard code it like that. I've been trying to develop a function that will loop through specific tables (they have a common naming convention) and return a table of records, but I'm not getting any results when I query the function. Function code is below: CREATE OR REPLACE FUNCTION public.internalid_formaltable_name

What is dynamic SQL?

ぐ巨炮叔叔 提交于 2019-11-27 19:39:47
I just asked an SQL related question, and the first answer was: " This is a situation where dynamic SQL is the way to go. " As I had never heard of dynamic SQL before, I immediately searched this site and the web for what it was. Wikipedia has no article with this title. The first Google results all point to user forums where people ask more or less related questions. However, I didn't find a clear definition of what a 'dynamic SQL' is. Is it something vendor specific? I work with MySQL and I didn't find a reference in the MySQL handbook (only questions, mostly unanswered, in the MySQL user

Function to loop through and select data from multiple tables

给你一囗甜甜゛ 提交于 2019-11-27 19:10:56
问题 I'm new to Postgres and have a database with multiple tables of the same structure. I need to select data from each table that matches certain criteria. I could do this with a bunch of UNION queries, but the number of tables I need to search can change over time, so I don't want to hard code it like that. I've been trying to develop a function that will loop through specific tables (they have a common naming convention) and return a table of records, but I'm not getting any results when I

Return dynamic table with unknown columns from PL/pgSQL function

百般思念 提交于 2019-11-27 17:44:14
问题 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

EXECUTE IMMEDIATE in plsql

我们两清 提交于 2019-11-27 17:01:10
问题 How to get a result from this code EXECUTE IMMEDIATE 'SELECT * FROM ' || table_name through for loop The usual method looks like this for items in (select * from this_table) loop htp.p(items.id); end loop; 回答1: If you really need to select * from dynamic table name , then I'd probably go with dbms_sql Type for a record : create type tq84_varchar2_tab as table of varchar2(4000); / Type for a result set (which is an array of records ): create type tq84_varchar2_tab_tab as table of tq84_varchar2

sql use statement with variable

时光毁灭记忆、已成空白 提交于 2019-11-27 14:18:22
I'm trying to switch the current database with a SQL statement. I have tried the following, but all attempts failed: USE @DatabaseName EXEC sp_sqlexec @Sql -- where @Sql = 'USE [' + @DatabaseName + ']' To add a little more detail. EDIT: I would like to perform several things on two separate database, where both are configured with a variable. Something like this: USE Database1 SELECT * FROM Table1 USE Database2 SELECT * FROM Table2 exec sp_execsql @Sql The DB change only lasts for the time to complete @sql http://blog.sqlauthority.com/2007/07/02/sql-server-2005-comparison-sp_executesql-vs

Why do I get “Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.” when I try to use sp_executesql?

南楼画角 提交于 2019-11-27 14:15:26
Why do I get this error Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'. when I try to use sp_executesql? Sounds like you're calling sp_executesql with a VARCHAR statement, when it needs to be NVARCHAR. e.g. This will give the error because @SQL needs to be NVARCHAR DECLARE @SQL VARCHAR(100) SET @SQL = 'SELECT TOP 1 * FROM sys.tables' EXECUTE sp_executesql @SQL So: DECLARE @SQL NVARCHAR(100) SET @SQL = 'SELECT TOP 1 * FROM sys.tables' EXECUTE sp_executesql @SQL The solution is to put an N in front of both he type and the SQL string to indicate it is a double-byte

How to search all text fields in a DB for some substring with T-SQL

邮差的信 提交于 2019-11-27 13:05:50
问题 I have a huge schema, with several hundreds of tables and several thousands of columns. I'd know that a specific IP address is stored in this database in several places, but I'm not sure what table(s) or column(s) it is stored in. Basically, I'm trying to find everywhere that this IP address is stored in the DB so I can update it to a new value in all those places. Here's my first crack at a T-SQL statement to print out the table and column name, and the value, for every text column in the

Test for null in function with varying parameters

拥有回忆 提交于 2019-11-27 12:57:12
I have a Postgres function: create function myfunction(integer, text, text, text, text, text, text) RETURNS table(id int, match text, score int, nr int, nr_extra character varying, info character varying, postcode character varying, street character varying, place character varying, country character varying, the_geom geometry) AS $$ BEGIN return query (select a.id, 'address' as match, 1 as score, a.ad_nr, a.ad_nr_extra,a.ad_info,a.ad_postcode, s.name as street, p.name place , c.name country, a.wkb_geometry as wkb_geometry from "Addresses" a left join "Streets" s on a.street_id = s.id left