I am unable to call a UDF in a Linked Server.
Here's my T-SQL code:
DECLARE #Phone_Digits_Input varchar(12);
DECLARE #Phone_Min_Max_Input varchar(5);
DECLARE #Phone_Digits_String varchar(10);
DECLARE #Phone_Min_Max_String varchar(5);
DECLARE #SQLString nvarchar(500);
DECLARE #ParmDefinition nvarchar(500);
DECLARE #phone_digits varchar(10);
SET #Phone_Digits_String = 'phone_digits';
SET #Phone_Min_Max_String = '10-10';
SET #SQLString = N'SELECT #phone_digitsOUT = RemoteServer.master.dbo.myUDF(#Phone_Digits_Input, #Phone_Min_Max_Input)';
SET #ParmDefinition = N'#Phone_Digits_Input varchar(10), #Phone_Min_Max_Input varchar(5), #phone_digitsOUT varchar(10) OUTPUT';
EXECUTE RemoteServer.master.dbo.sp_executesql #SQLString, #ParmDefinition, #Phone_Digits_Input = #Phone_Digits_String,
#Phone_Min_Max_Input = #Phone_Min_Max_String,
#phone_digitsOUT=#phone_digits OUTPUT;
SELECT #phone_digits;
When I run it, I get a message:
Msg 344, Level 16, State 1, Line 1
Remote function reference 'RemoteServer.master.dbo.myUDF' is not allowed, and the column name 'RemoteServer' could not be found or is ambiguous.
How can I fix the issue please?
I got the server name wring! The UDF was i a different database! Sorry for my confusion.
The final code is below:
DECLARE #Phone_Digits_Input varchar(12);
DECLARE #Phone_Min_Max_Input varchar(5);
DECLARE #Phone_Digits_String varchar(10);
DECLARE #Phone_Min_Max_String varchar(5);
DECLARE #SQLString nvarchar(500);
DECLARE #ParmDefinition nvarchar(500);
DECLARE #phone_digits varchar(10);
SET #Phone_Digits_String = 'phone_digits';
SET #Phone_Min_Max_String = '10-10';
SET #SQLString = N'SELECT #phone_digitsOUT = dbo.MyUDF(#Phone_Digits_Input, #Phone_Min_Max_Input)';
SET #ParmDefinition = N'#Phone_Digits_Input varchar(10), #Phone_Min_Max_Input varchar(5), #phone_digitsOUT varchar(10) OUTPUT';
EXECUTE MyServer.MyDB.dbo.sp_executesql #SQLString, #ParmDefinition, #Phone_Digits_Input = #Phone_Digits_String,
#Phone_Min_Max_Input = #Phone_Min_Max_String,
#phone_digitsOUT=#phone_digits OUTPUT;
SELECT #phone_digits;
Thank you.
Related
I have a piece of code:
DECLARE #v int;
DECLARE #SQLString nvarchar(500);
DECLARE #ParmDefinition nvarchar(500);
DECLARE #max_title varchar(30);
SET #IntVariable = 197;
SET #SQLString = N'SELECT #max_titleOUT = max(JobTitle)
FROM some_table
WHERE name = #level';
SET #ParmDefinition = N'#level tinyint, #max_titleOUT varchar(30) OUTPUT';
EXECUTE sp_executesql #SQLString, #ParmDefinition, #level = #v, #max_titleOUT=#max_title OUTPUT;
SELECT #max_title;
I'm a little bit confused about this line:
WHERE name = #level';
If #v is a string instead of an int variable, should I put quotes around #level like this?
WHERE name = ''#level''';
When should I put quotes around a variable inside the #SQLString?
No, #level and #v are variables. You shouldn't put quotes around.
When should I put quotes around a variable inside the #SQLString?
Nowhere
I'm trying to create a stored procedure. Here's a short version of my code:
CREATE PROCEDURE foobar
#table_name nvarchar(20),
#Work_Status nvarchar(20)
AS
BEGIN
update #table_name -- /// How Do I do this? ///
set work_status = #Work_Status
END
How can I define the table name via a parameter?
Try to use a dynamic query:
DECLARE #SQL NVARCHAR(4000)
DECLARE #ParamDefinition nvarchar(500)
SET #ParamDefinition = N'#Work_Status nvarchar(20), #day_no nvarchar(10), #day_month nvarchar(10), #day_years nvarchar(10)';
--...CURSOR declaration
--... BEGIN
SELECT #SQL =
N'update [SKTH_ENSUSER].[dbo].' + #tbl_name + '
set t_work_status = #Work_Status
where t_day_no = #day_no and t_month_no = #day_month and t_year_no = #day_years'
EXECUTE sp_executesql #SQL, #ParamDefinition, #Work_Status = #Work_Status, #day_no = #day_no, #day_month = #day_month, #day_years = #day_years
--...
-- FETCH NEXT FROM ENS_cursor
Updated: #SQL must be NVARCHAR
I am calling a stored procedure like this:
EXEC usp123 #F1 = 1, #F2 = 'A', #F3 = 5
DECLARE #Result int
SET #Result = (EXEC usp123 #F1 = 1, #F2 = 'A', #F3 = 5)
How do I assign a variable to the output of this stored procedure as the above syntax does not work ..
The last line should be
Declare #Result Int
EXEC #Result = usp123 #F1 = 1, #F2 = 'A', #F3 = 5
--Check this... your answer is in the last sentence.
DECLARE #IntVariable int;
DECLARE #SQLString nvarchar(500);
DECLARE #ParmDefinition nvarchar(500);
DECLARE #max_title varchar(30);
SET #IntVariable = 197;
SET #SQLString = N'SELECT #max_titleOUT = max(JobTitle)
FROM AdventureWorks2012.HumanResources.Employee
WHERE BusinessEntityID = #level';
SET #ParmDefinition = N'#level tinyint, #max_titleOUT varchar(30) OUTPUT';
EXECUTE sp_executesql #SQLString, #ParmDefinition, #level = #IntVariable,
#max_titleOUT=#max_title OUTPUT;
SELECT #max_title;
USE GDMDBNS_1720
GO
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE'
AND ROUTINE_SCHEMA = 'dbo' AND ROUTINE_NAME = 'usp_RmsExecuteValidationRule')
BEGIN
DROP PROCEDURE dbo.usp_RmsExecuteValidationRule
END
GO
Create PROCEDURE [dbo].[usp_RmsExecuteValidationRule]
#nRuleId INT,
#FIPSName VARCHAR(5)
AS
BEGIN
DECLARE #strRuleQuery VARCHAR(MAX)
DECLARE #sqlstat AS NVARCHAR(MAX)
DECLARE #params AS NVARCHAR(MAX)
SET #strRuleQuery = N'SELECT RULE_QUERY from GdmValidationRuleMaster where
RULE_ID = #nRuleId'
SET #sqlstat = #strRuleQuery
SET #params = N'#FIPSName VARCHAR(5)'
EXEC sp_executesql #params,
#query = #sqlstat,
#FIPSName = #FIPSName
END
got error while Debugging must declare scalar variable #nRuleId . I have already declared. But error is pooping out always.
There appear to be a number of issues with the code, as is.
To return a value, you need to declare an OUTPUT parameter and the variable #FIPSName is not actually used in the code, and #strRuleQuery is superfluous in this code.
Here is the code from inside the SProc, that should be closer to your needs.
-- Parameters
DECLARE
#nRuleId INT = 1,
#FIPSName VARCHAR(5) = 'ITS'
-- Local variables
DECLARE #sqlstat AS NVARCHAR(MAX)
DECLARE #params AS NVARCHAR(MAX)
DECLARE #RULE_QUERY NVARCHAR(MAX)
SET #sqlstat = N'SELECT #RULE_QUERY=RULE_QUERY from GdmValidationRuleMaster where RULE_ID = #nRuleId'
SET #params = N'#nRuleId INT, #RULE_QUERY NVARCHAR(MAX) OUTPUT'
EXEC sp_executesql
#sqlstat,
#params,
#nRuleId = #nRuleId, #RULE_QUERY= #RULE_QUERY OUTPUT
-- Output
SELECT #RULE_QUERY
You must rewrite execution part:
SET #params = N'#FIPSName VARCHAR(5), #nRuleId int'
EXEC sp_executesql #strRuleQuery, #params, #FIPSName = #FIPSName, #nRuleId = #nRuleId
I don't understand, why do you pass #FIPSName as parameter, it is not used in your batch.
I write a query in sql server i am getting value in a temporary variable, this is my query
declare #tacolumn1 varchar(50)='cargo'
declare #temp1 varchar(50)
declare #sql nvarchar(max)
print #tacolumn1
set #sql= ('select '+#tacolumn1+' from operatingincome where YEAR(createddate)= 2009')
exec sp_executesql #sql
print #temp1
from the above #sql has the value , now i want to save that value in #temp1 how can i do that one
sp_executesql procedure can have input and output parameters:
DECLARE #IntVariable int;
DECLARE #SQLString nvarchar(500);
DECLARE #ParmDefinition nvarchar(500);
DECLARE #max_title varchar(30);
SET #IntVariable = 197;
SET #SQLString = N'SELECT #max_titleOUT = max(JobTitle)
FROM AdventureWorks2008R2.HumanResources.Employee
WHERE BusinessEntityID = #level';
SET #ParmDefinition = N'#level tinyint, #max_titleOUT varchar(30) OUTPUT';
EXECUTE sp_executesql #SQLString, #ParmDefinition, #level = #IntVariable, #max_titleOUT=#max_title OUTPUT;
SELECT #max_title;