dynamic-sql

How do I use variables in a postgresql function for loop query

拈花ヽ惹草 提交于 2021-02-07 18:32:00
问题 I have a rather complicated function in postgresql (Version 9.4.4) that I need a bit of help with. I have a loop (with lots of work below) declared like this inside of my function: CREATE OR REPLACE function getRSI( psymbol varchar, pstarttime timestamp with time zone, pendtime timestamp with time zone, pduration double precision, ptable varchar ) RETURNS SETOF rsi AS $BODY$ declare row_data record; -- some variables begin FOR row_data IN SELECT datetime, value FROM "4" WHERE symbol = 'AAPL'

Escape single quote in openquery using dynamic query

北慕城南 提交于 2021-02-07 13:47:58
问题 I need to retrieve data from a linked server using a parameter, @PickedDate for example. The query works fine if I skip @A and @B, but it always returns an error due to a missing single quote. Please advise, thanks. The query: Declare @OPENQUERY nvarchar(500), @TSQL nvarchar(max), @LinkedServer nvarchar(20), @A varchar(5), @B varchar(5), @PickedDate varchar(8) Set @PickedDate = '20150501' Set @A = 'AAA' Set @B = 'BBB' Set @LinkedServer = 'LinkedServerName' Set @OPENQUERY = 'Select * From

Escape single quote in openquery using dynamic query

会有一股神秘感。 提交于 2021-02-07 13:47:06
问题 I need to retrieve data from a linked server using a parameter, @PickedDate for example. The query works fine if I skip @A and @B, but it always returns an error due to a missing single quote. Please advise, thanks. The query: Declare @OPENQUERY nvarchar(500), @TSQL nvarchar(max), @LinkedServer nvarchar(20), @A varchar(5), @B varchar(5), @PickedDate varchar(8) Set @PickedDate = '20150501' Set @A = 'AAA' Set @B = 'BBB' Set @LinkedServer = 'LinkedServerName' Set @OPENQUERY = 'Select * From

Escape single quote in openquery using dynamic query

倖福魔咒の 提交于 2021-02-07 13:47:02
问题 I need to retrieve data from a linked server using a parameter, @PickedDate for example. The query works fine if I skip @A and @B, but it always returns an error due to a missing single quote. Please advise, thanks. The query: Declare @OPENQUERY nvarchar(500), @TSQL nvarchar(max), @LinkedServer nvarchar(20), @A varchar(5), @B varchar(5), @PickedDate varchar(8) Set @PickedDate = '20150501' Set @A = 'AAA' Set @B = 'BBB' Set @LinkedServer = 'LinkedServerName' Set @OPENQUERY = 'Select * From

Dynamic SQL table name as variable

↘锁芯ラ 提交于 2021-01-29 14:35:50
问题 I have this procedure which is working, 2 parameters can be passed when calling procedure and it executes the select query. create or replace procedure dynamic_sql (input1 varchar2, input2 varchar2) as begin execute immediate 'select :variable1, :variable2 from emp' using input1,input2; end; / exec dynamic_sql('ename','job'); In the same way I try to add third variable which will replace the table Emp, but it doesn't work, passed in table name is 100% correct. This is the code that doesn't

Dynamic SQL Server query

霸气de小男生 提交于 2021-01-29 04:26:03
问题 I am writing a dynamic insert query in a stored procedure. I am receiving the column names as parameter to my stored procedure. For example, I have an Employee table with EmployeeId and EmployeeName columns. I need to append EMP_ before each employee name while inserting the data into Department table from Employee table. Non-dynamic query looks like this. INSERT INTO Department(EmployeeId, EmployeeName) SELECT EmployeeId, 'EMP_' + EmployeeName FROM Employee If I write a dynamic insert SET

Creating SQL table using dynamic variable name

▼魔方 西西 提交于 2021-01-20 18:09:43
问题 I want to create backup SQL tables using variable names. something along the lines of DECLARE @SQLTable Varchar(20) SET @SQLTable = 'SomeTableName' + ' ' + '20100526' SELECT * INTO quotename(@SQLTable) FROM SomeTableName but i'm getting Incorrect syntax near '@SQLTable'. It's just part of a small script for maintence so i don't have to worry about injections. 回答1: DECLARE @MyTableName nvarchar(20); DECLARE @DynamicSQL nvarchar(1000); SET @MyTableName = "FooTable"; SET @DynamicSQL = N'SELECT *

Creating SQL table using dynamic variable name

早过忘川 提交于 2021-01-20 18:08:23
问题 I want to create backup SQL tables using variable names. something along the lines of DECLARE @SQLTable Varchar(20) SET @SQLTable = 'SomeTableName' + ' ' + '20100526' SELECT * INTO quotename(@SQLTable) FROM SomeTableName but i'm getting Incorrect syntax near '@SQLTable'. It's just part of a small script for maintence so i don't have to worry about injections. 回答1: DECLARE @MyTableName nvarchar(20); DECLARE @DynamicSQL nvarchar(1000); SET @MyTableName = "FooTable"; SET @DynamicSQL = N'SELECT *

Stored Procedure to Copy Views from Current Database to Another

北城以北 提交于 2020-12-12 10:51:10
问题 I've been asked whether it's possible to create a stored procedure that will copy all views in the current database to another one (named via stored procedure parameter). For context, all databases have the same schemas. This situation arises thanks to a 3rd party risk modelling tool that generates each run's output as an entirely new database (rather than additional rows in an existing database). The user wants an easy way to "apply" their 20 or so custom views (from their "Template"