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
0 answers
Ageing SQL query show incorrect syntax
I have below query which is for ageing report, but it got an error in one line of query where I cant find out the cause.
DECLARE @sql nvarchar(MAX)
SET @sql = N'SELECT
deb.code[Original Deb Code],
IIF(deb.Code =…

Bubble Bub
- 651
- 4
- 12
- 32
0
votes
1 answer
Dynamic SQL table validation for data quality dimension
I have the following code to test for nulls in a whole table using dynamic sql:
/*Completitud*/
--Housekeeping:
drop table if exists tmp_completitud;
--Declarar variables para el loop:
declare @custom_sql VARCHAR(max)
declare @tablename as…

metarodri
- 1
- 1
0
votes
0 answers
Select in select query in SQL Server
I have a table(validation_number) that contains an unique id and comma separated name of columns:
Table is in similar fashion
The column name are the column names of another table(Validation )which I want to show or hide based on the validation…
0
votes
1 answer
Passing variable inside stored procedure in Redshift returning error
I have this stored procedure that I can successfully compile, but upon calling the procedure, I get an error:
Amazon Invalid operation: syntax error at or near "$1"
Can you please help me understand what is wrong? Thank you in advance.
CREATE OR…

SusanD
- 143
- 9
0
votes
2 answers
ORA-01008: not all variables bound - "not all variables bound"
This is my plsql block, dynamic SQL. I can't find a reason why it comes up with an error 'no ORA-01008: not all variables bound ORA-06512: at line 23'.
I can't find the error on my EXECUTE IMMEDIATE statement.
DECLARE
form_name …

ebbie
- 1
- 3
0
votes
1 answer
add parameter to dynamic EXECUTE in postgreSQL
I have a procedure which contain this piece of code
SELECT query INTO query FROM temp_table_test WHERE id = whateverId;
EXECUTE query;
My query variable will contain something like
CALL Someprocedure (withSomeParameters)
How can i make the…

MattRgx
- 35
- 8
0
votes
1 answer
Using a dynamic variable that may be null in the where clause of a mysql statement
I select a value into a mysql variable. Eg...
select @userId := userId
from myTable
where id = ?;
The value of @userId may be null.
Now I want to update the rows in another table based on the value of @userId.
Simply…

user1031947
- 6,294
- 16
- 55
- 88
0
votes
3 answers
Assign output of dynamic SQL to a variable (without stored proc)
I have the code below, where I want to get the id of a row via dynamic SQL.
Please note that variables @posselect and @catselect are filled, but I left that out for overview.
declare @SQL nvarchar(1000)
declare @posselect nvarchar(50)
declare…

Adam
- 6,041
- 36
- 120
- 208
0
votes
2 answers
How to Pass columns contained in a declared variable in SQL Server
I have a table Depots that looks like this:
DepotID
DepotName
DepotLocation
DepAlias
1
Ouland
Utsacity
Oula
2
Ingri
Utsacity
Inglas
3
Turks
Utsacity
Turku
4
tamps
Uusimaa
Tampere
5
Kokos
Uusimaa
Kokoola
6
Kaus
Olomba
Kaukana
I…

Mikko
- 29
- 1
- 7
0
votes
1 answer
How to properly insert Timestamp using plpgsql and EXECUTE format...USING dynamic SQL
I have a dynamic SQL statement I'm building up that is something like
my_interval_var := interval - '60 days';
sql_query := format($f$ AND NOT EXISTS (SELECT 1 FROM some_table st WHERE st.%s = %s.id AND st.expired_date >= %L )$f$, var1, var2, now()…

ennth
- 1,698
- 5
- 31
- 63
0
votes
1 answer
Error while passing a string parameter to a case statement in dynamic SQL
I am trying to build a variable by passing a string parameter to evaluate a case expression and execute a dynamic SQL. But it is erroring out with: **Invalid Column Name 'abc'**
'abc' is being read as a column instead of a string, fiddled around…

Kiran
- 27
- 5
0
votes
2 answers
"Nesting" variables in Dynamic SQL - How do I keep valid syntax?
I apologize if this has been asked and I am to dense to grok the answer.
I am trying to determine the best way to do the following:
Declare @sql nvarchar(4000)
, @Filter nvarchar(500)
SET @filter = 'WHERE value1 IN (''A'',''B'',''C'')'
SET @sql =…

Forrest Pugh
- 31
- 5
0
votes
1 answer
AWS Redshift dynamically select column name from RECORD
I have created below procedure in AWS redshift.
In the query2 (at ????) I want to select the column from rec based on value provided in field input variable.
e.g. if field = 'Fname' then in query2 it should insert rec.Fname.
Please let me know how…

Prithvi
- 1
- 1
0
votes
1 answer
How can create a table while Execute dynamic @Query
I have a dynamic query in my sp which has different columns related to the inputs that user will enter. After that, In sp I want to join the result of it to the remaining part of sp. So I should have the table for join and as I mentioned I can not…

fahime abouhamze
- 328
- 3
- 16
0
votes
1 answer
How to rollup records having not null values for each column in db2 Sql?
I have a table as below-
id
name
col1
col2
col3
1
aaa
1
null
null
1
aaa
null
1
null
1
aaa
null
null
1
1
aaa
null
null
2
1
aaa
null
1
null
2
bbb
null
null
null
2
bbb
1
null
null
i want the output as…

Salva
- 81
- 1
- 9