Dynamic SQL is a technique using SQL (Structured Query Language) whose main difference from traditional SQL is that Dynamic SQL allows to build SQL statements dynamically at runtime, which eases the automatic generation and execution of program statements.
Questions tagged [dynamic-sql]
2681 questions
0
votes
1 answer
T-SQL Dynamic pivot query and SQL Injection protection
When importing/parsing flat files and delimited data (we use SqlBulkCopy for these) we end up with a table with a DATA_KEY and DATA_VAL column. [EDIT] Forgot to mention this table is a work table for the .net crl to parse the various inbound data…

A. Guattery
- 113
- 8
0
votes
1 answer
PostgreSQL error in creating function with varying columns using RECORD
I have this function that should be returning a varying columns, can I get a help on what is wrong with my code?
CREATE OR REPLACE FUNCTION functions.search(column_name VARCHAR(40))
RETURNS SETOF RECORD AS $$
DECLARE
rec RECORD;
BEGIN
…

driedbananas
- 9
- 4
0
votes
1 answer
Why parameter does not work in PostgreSQL function in from clause's table name?
I need to use PostgreSQL function like below but it does not work. Any helps?!
I need to make below function not "select count(*) from table_name"
create or replace function count_rows_of_table(table_name text)
returns numeric
language…

TWpower
- 39
- 3
0
votes
1 answer
'Msg 137, Level 16, State 1, Line 17 Must declare the scalar variable "@tempFinalTable"' error but @tempFinalTable is declared
(Working with Microsoft SQL Server Management Studio)
I have a query that should, in theory, return a table containing all of the tables contained within the databases of the server to which I am connected.
However, whenever I run the query, I get…

Sazed
- 1
- 2
0
votes
1 answer
T-SQL (Azure Synapse) Dynamic grouping based on unknown number of Primary Keys
I'm tasked with creating a sp that will loop through every table in a supplied schema and find any that do not have unique primary keys (duplicate primary keys). Some tables may have only one column creating the primary key while others may have…

haredev
- 67
- 1
- 10
0
votes
3 answers
Multiple table outputs based on one query
I have one table that has 12 conditions (months). I need to produce 12 outputs (tables) for select statements where the only thing that changes is that one condition (month).
Example:
SELECT * FROM table WHERE month = 01;
SELECT * FROM table WHERE…

Sqrrrl
- 1
0
votes
1 answer
Set variable in SQL query
I am trying to set variable but I get Incorrect syntax near '='..
DECLARE @name NVARCHAR(100) = 'Some name'
DECLARE @id INT
EXECUTE ('SET ' + @id + ' = (SELECT mm.mmID FROM dbo.measurem AS mm
WHERE mm.placeName = ''' + @name +…

Hrvoje T
- 3,365
- 4
- 28
- 41
0
votes
1 answer
How to solve error Conversion failed when converting the nvarchar value to data type integer?
I work on SQL Server 2014 with a dynamic query; I get this error when I run the query:
Conversion failed when converting the nvarchar value '24VAC/DC' to data type int.
I got error when executing this dynamic SQL
EXEC (@SQL)
How to solve this…

abeer shlby
- 41
- 6
0
votes
0 answers
Snowflake - Is there any way to concatenate a SQL query using UDF defined in JavaScript to a where clause inside another UDF defined in SQL?
I am trying to create a UDF2 in JavaScript language which returns set of query statements as a String based on the input we pass. Here , this String value should be appended to the where clause as SQL Text in UDF1 defined in SQL language.
for…

Mounika
- 1
0
votes
1 answer
PostgreSQL dynamic query: find most recent timestamp of several unrelated tables
I have a number of tables, and many of them have a timestamp column. I can get a list of every table with a timestamp column:
SELECT table_name
FROM information_schema.columns
WHERE table_schema='my_schema' AND…

Kaia
- 862
- 5
- 21
0
votes
1 answer
Dynamic start date from specific column in a table (sysdate)
I am pretty new in this field, trying to learn slowly so please be patient with me :)
My database contains a table called t_usage_interval. In this table there is a column name ID_Interval. Each month a new random 10 digit number is created in this…

Asireb Rimas
- 3
- 2
0
votes
1 answer
How to execute a SQL string returned from a stored procedure in Postgresql?
I have a simple stored procedure below which is returning a SQL string:
CREATE OR REPLACE PROCEDURE dbo.select_users_test(sqlstring inout text)
LANGUAGE plpgsql
AS
$$
BEGIN
sqlstring = 'select * from dbo.user where usr_key in (1,…

Fakhar Ahmad Rasul
- 1,595
- 1
- 19
- 37
0
votes
0 answers
Execute immediate with dynamic sql in Google BigQuery
Following this question, I was trying to create a dynamic SQL statement with a pivot table and a CTE in Google BigQuery.
This worked straight of the box (with CTE and pivot):
with Produce AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter…

rabbit
- 81
- 6
0
votes
1 answer
In clause with a list of declared variables PostgreSQL
I have this:
CREATE or REPLACE FUNCTION name_list(name_of_interest text)
RETURNS table (name text,
sex text,
age int,
state text)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN query
SELECT name, sex, age, state…

M Shen
- 51
- 5
0
votes
1 answer
Creating a Query for each row in a SQL Table
I have a schema in my SQL table out of which some table has a time value stamp (same column name 'timestamp' in all the tables in the schema) and I need to create a new table which will give the latest time stamp for each such table. I have…

Stan
- 786
- 1
- 9
- 25