Passing variable in OPENQUERY for a linked server name - sql-server-2008-r2

How can I pass Linked server name as a variable?
something like this:
declare #A varchar(55) = 'LinkedServerName'
select * from OPENQUERY(#A,'select 1')

This helped. Here I have to create variable for the whole openquery, and than excute it by dynamic SQL.
https://support.microsoft.com/en-us/kb/314520

Related

Azure Data Factory - Lookup Activity

I'm calling a procedure using a lookup activity in Azure Data Factory.
NOTE: The reason to use Lookup here is, I wanted to store the OUTPUT parameter value from procedure into a variable in ADF for future use.
Below works,
DECLARE #ADFOutputMsg [VARCHAR](500);
EXEC Test.spAsRunTVA #ReportDate = '2022-06-01', #OutputMsg = #ADFOutputMsg OUTPUT;
SELECT #ADFOutputMsg As OutputMsg;
But when I want to pass dynamic parameters, it doesn't like,
DECLARE #ADFOutputMsg [VARCHAR](500);
EXEC #{pipeline().parameters.SchemaName}.spAsRunTVA #ReportDate = #{substring(pipeline().parameters.FileName,8,10)}, #OutputMsg = ADFOutputMsg OUTPUT;
SELECT #ADFOutputMsg As OutputMsg;
I also tried to keep the date As-Is and just updated SchemaName to be dynamic but I still get the error.
Please Provide single quote ' ' at your dynamic content
'#{substring(pipeline().parameters.FileName,8,10)}'
I tried to reproduce similar kind of approach in my environment and I got below results:
Use the below dynamic content in the query with lookup activity. and also Added dynamic content with single quotes ' '
select * from for_date where date1='#{string(pipeline().parameters.Date)}'
Added Date Parameter
Got this Output:

DB2 procedure call literal error

I'm trying to call a DB2 procedure using DBeaver.
The syntax I'm using is this:
{ call db.procedure_name ('1234','2345','3456') }
Then I get an error saying:
"Literal replacement prasing failed for procedure call... Failing SQL
text..."
When I call the same procedure like this:
{ call db.procedure_name (?,?,?) }
and insert the parameters manually it executes.
I'm quite new to DB2 and IBM troubleshooting sites aren't much of a help.
Can you guys help me out? Thanks.
Try call db.procedure_name (:1,:2,:3)
or alternatively, create global variables for each parameter
create or replace variable p1 integer default 123;
create or replace variable p2 decimal(5,2) default 123.45;
create or replace variable p3 char(5);
set p3 = '12345';
call db.procedure_name (p1,p2,p3);

How to store the result of a select query into a variable(IBM DB2)?

I am trying to save the result of a query into a variable. I am using IBM DB2, but I can only store the result if I am declaring the variable inside a procedure.
My code is:
DECLARE #myvar INTEGER;
SET #myvar = (SELECT MAX(ID) FROM S0SCSQMS.S0SRPTCNAME);
and I receive the following errors:
For the first line:"SQL0104N An unexpected token "INTEGER" was found following "DECLARE #myvar ". Expected tokens may include: "END-OF-STATEMENT". LINE NUMBER=1. SQLSTATE=42601"
The error code does not tell me much. I looked for it on the IBM documentation.
Looking forward for an answer.
Thank you.
try this (work on iseries db2 v7r1)
CREATE OR REPLACE VARIABLE myvar INTEGER ;
SET myvar = (SELECT max( id_xp_dossier) FROM cilgprod.xp_dossier);
DROP VARIABLE myvar;

Is there anyway to reference the collection of parameters passed to a stored procedure, without referencing each one by name?

Is there any way to reference the collection of parameters passed to a stored proc, without referencing each one by name?
The context here is error logging. I'd like to develop a generic CATCH clause, or sub-clause, that grabs all parameter values as well as other error and execution info and logs it to a table (one or more).
The basic version looks something like this:
CREATE PROC dbo.Proc1 #Param1 INT, #Param2 INT
AS
BEGIN TRY
SELECT 1/0
END TRY
BEGIN CATCH
DECLARE #params XML
SET #params = (
SELECT
#Param1 AS [Param1]
, #Param2 AS [Param2]
FOR XML PATH('params'), TYPE
)
EXEC dbo.LogError #procid = ##PROCID, #params = #params
EXEC dbo.RethrowError
END CATCH
Now, this template does work, except that for each individual procedure, I would have to edit the SET #params section manually.
I could script it out easily enough, but then I would still need to copy it in.
I could leave some sort of placeholder, and then dynamically update each definition w/ a correct SET statement. I could even write a database trigger that listens for CREATE and ALTER PROC statements and have it do this for me automagically.
But what I really want is just to grab the collection of parameters directly and be done with it. Is there any way?
EDIT: getting parameter metadata from the system tables is not enough. For example:
DECLARE #sql NVARCHAR(MAX)
SET #sql =
'SET #params = (SELECT '
+ STUFF((
SELECT ', '+name+' AS '+STUFF(name,1,1,'')
FROM sys.parameters WHERE object_id = ##PROCID
FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)')
,1,2,'')
+ ' FOR XML PATH(''params''), TYPE)'
-- inline execute here....alas, there's isn't a mode for that
EXEC sp_executesql #sql, N'#params XML OUTPUT', #params OUTPUT
This, of course, generates an error, since I haven't declared parameters for the nested scope of sp_executesql. While I could script out the second param of sp_executesql, I can't assign them without explicitly assigning them one at a time by name.
Original question, rephrased: is there another flavor of sp_executesql that inherits variable "context" from the outer scope, that executes in the same frame, that inlines the command, rather than executing in a nested scope?
Why not use sys.dm_exec_sql_text(#sql_handle) to retrieve the SQL command, using sys.sysprocesses.sql_handle?

How do I use a variable for the name of a table in a DBI query?

How do I using a variable for the name of a table in a DBI query? I know how to use placeholders as part of the where clause, but how do I do this for the table name?
I would like to do something like this:
my $table_name='table1';
my $query = $dbh_cgi->prepare("select * from ?");
$query->execute($table_name);
So far, I end up getting a MySQL syntax error because DBI adds quotes around the name, table1.
One of the limitations of placeholders is that they can't be used for table names. Instead of using a placeholder, you can use a variable. To make sure the variable's contents are safe, use quote_identifier(), for example:
my $table_name='table1'; #-- Or get this from somewhere else
my $safe_table_name = $dbh_cgi->quote_identifier($table_name);
my $query = $dbh_cgi->prepare("select * from $safe_table_name");
$query->execute();