I'm having trouble getting this SP call to work. It worked previously and no changes were made, but I cannot get it to work for the life of me now.
The error code I'm getting is:
Error code 137, SQL state S0002: Must declare the scalar variable "#AppName".
This is using netbeans sql editor. if I hard code the AppName variable, it then throws the same error for AppVer. A coworker can use the exact code and it works fine in his non-netbeans ide (I think vis studio?). This leads me to believe that it's a netbeans issue rather than the code itself.
Any ideas?
DECLARE #AppName nvarchar(100) = 'Excel ASAP Utilities';
DECLARE #AppVer nvarchar(50) = '3.07b';
DECLARE #AppManufacture nvarchar(100) = null;
DECLARE #RequestedByID dbo.UserID = 'userid';
DECLARE #RequestDescription nvarchar(3500) = 'This is a test';
DECLARE #NumberNeededFor int = null;
DECLARE #AssignToID dbo.UserID = '?';
DECLARE #RequestType_ID int = 6;
DECLARE #LCM_ID int = 50;
DECLARE #ProcessID int = 3;
DECLARE #RM_ID int;
EXECUTE dbo.Request_InsertNewShortForm
#AppName,
#AppVer,
#AppManufacture,
#RequestedByID,
#RequestDescription,
#NumberNeededFor,
#AssignToID,
#RequestType_ID,
#LCM_ID,
#ProcessID,
#RM_ID OUTPUT;
SELECT 'Return Value' = #RM_ID;
Related
I have the following code in Sql Server 2019.
declare #Id bigint
declare #HexId varchar(50)
set #Id = 98360090175733911
set #HexId = CONVERT(VARCHAR(50),CONVERT(VARBINARY(16),#Id),2)
select #HexId
This is working, but the result must be in little endian.
Can someone help me with this problem?
Kind regards,
Bert Berrevoets.
I have tried the reverse function, but this was not ok.
to get the string i used the internal function master.dbo.fn_varbintohexstrit seems to be the format you wanted
declare #Id bigint
declare #HexId varchar(50)
set #Id = 98360090175733911
set #HexId = master.dbo.fn_varbintohexstr(CONVERT(BINARY(16), REVERSE(CONVERT(BINARY(16), #Id))))
select #HexId
Get you
0x9700001bfb715d010000000000000000
I am trying to run the below script in SQL Server Management Studio:
DECLARE #counter INT = 1
EXEC('SET '+#counter+' = '+#counter+' + 1')
However, I get this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '1'-
I have been troubleshooting for days but gotten nowhere - I guess most likely TSQL simply does not accept this inside an EXEC command, but what could I potentially replace it with?
This is a part of a larger script where the above MUST be inside an EXEC statement in order for the rest to work..
This isn't going to working for a number of reasons.
Let's start with what you have:
DECLARE #counter INT = 1
EXEC('SET '+#counter+' = '+#counter+' + 1')
EXEC is expecting a string inside the parenthesis, yet you have an int (the variable #counter), so that is your first problem. Oddly, however, you aren't getting a conversion error here; it seems that #counter is being interpreted as its value (1), rather than Data Type Precedence being applied first. (If you try SELECT 'SET '+#counter+' = '+#counter+' + 1' you'll get the error "Conversion failed when converting the varchar value 'SET ' to data type int.").
Even if the query successfully got past that hurdle, that would cause another error because the query becomes:
SET 1 = 1 + 1
Obviously that isn't going to work, 1 is 1, no other number.
Next, a variable declared outside of the scope of a "dynamic" statement can't be referenced inside it. Let's take the following:
DECLARE #Varible int = 1;
EXEC (N'SELECT #Variable;');
This returns the error:
Must declare the scalar variable "#Variable".
If you are using a "dynamic" statement (your statement isn't dynamic at all), then you need to parametrise the variables. This is why you shouldn't be using EXEC ({SQL Statement}) and should be using sys.sp_executesql.
So, for the above, you get:
DECLARE #counter INT = 1;
DECLARE #SQL nvarchar(MAX);
SET #SQL = N'SET #Variable = #Variable + 1;';
EXEC sys.sp_executesql #SQL, N'#Variable int OUTPUT',#Variable = #counter OUTPUT;
SELECT #Counter;
As I mentioned, that is all pointless, as there's nothing dynamic in the above, and your query should just be:
DECLARE #counter INT = 1;
SET #counter = #counter + 1;
Of course, none of this even goes on to address things like how to safely inject objects into a dynamic statement, but that isn't what this question is about. As I mention, the dynamic statement here is redundant, as it isn't dynamic, and it's not that required deferred parsing (such as when you ALTER a table to add a column, and then try to reference that column in the same batch).
If you run the script below, it'll throw an error like
Msg 137, Level 15, State 1, Line 1
Must declare the scalar variable "#counter".
BEGIN
DECLARE #counter INT = 1
EXEC('SET #counter = '+ #counter+' + 1')
END
WHY?
Because dynamic SQL is a different scope to the outer.
So can try this way (using output)
DECLARE #counter INT = 1
DECLARE #SQL nvarchar(50) = 'SELECT #counter2 =' + CAST(#counter + 1 AS VARCHAR(100))
exec sp_executesql #SQL, N'#counter2 int out', #counter out
SELECT #counter
I have looked through countless posts on declaring/setting variables in a sql script. But most seem to involve syntax errors or using exec commands. The script I am trying to execute is quite simple - so I am having a hard time understanding why I cant set values.
Here is the SQL:
declare #counter int = 1,
#batchSize int = 100000,
#tableRows int,
#totalBatches int;
set #tableRows= (select count(distinct chFileVersionID) from IRISDocuments)
set #totalBatches = (ceiling((#tableRows / #batchSize)));
--print some stuff
while #counter <= #totalBatches
begin
. . . loop logic that only uses #counter variable for incrementing
end
The error I get is
Must declare the scalar variable "#tableRows"
which is clearly declared directly above. I have tried setting values with a select statement, as well as declaring each variable individually, and declaring and setting value in the same statement with no avail.
The above actually works (I mean testing without the WHILE loop of course).
2 possible source of the error if the variable is not exactly used as above:
Using a variable to define another variable in the same declaration
E.g.:
declare #counter int = 1,
#batchSize int = 100000,
#tableRows int = 10000000000000,
#totalBatches int = (ceiling((#tableRows / #batchSize)))
This won't work and produces your error - within a statement the order of execution is not row-by-row, so you can not give a value to #totalBatches as at that point #tableRows is not implemented yet.
Trying to use a variable that is out of scope
If this is a query that contains the GO batch separator then after that all variables before GO are out of scope.
E.g.:
declare #counter int = 1,
#batchSize int = 100000,
#tableRows int,
#totalBatches int;
SELECT #counter, #batchSize, #tableRows, #totalBatches int
GO
SELECT #counter, #batchSize, #tableRows, #totalBatches int
The second SELECT will throw your error.
I have this line of SQL:
DECLARE #temp_Id UNIQUEIDENTIFIER = #data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier')
So, #data is a parameter to my stored procedure. #data XML
I want to pass a value out of the XML into another stored procedure, as you can see it's the Id field in the xml.
#data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier')
This pulls out and converts the Id value to a Uniqueidentifier however when I assign this to a variable and pass it to the stored procedure:
EXEC my_sproc #Id = #temp_Id
I get an error
Msg 137, Level 15, State 2, Procedure Some_Sproc, Line __
Must declare the scalar variable "#tempId".
Looking around, people are suggesting to wrap the #data.value(... in a CONVERT( DATA_TYPE, VALUE ) which seems slightly absurd.
-- EDIT --
I reckon there could also be a chance of my SQL Server Management Studio intellisense being out of sync....
-- CODE --
CREATE PROCEDURE [dbo].[Sproc_CB8PutNotification]
#message_type NVARCHAR(250),
#utc_timestamp DATETIME2,
#data XML
AS
BEGIN TRY
DECLARE #temp_Id UNIQUEIDENTIFIER = #data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier')
EXEC dbo.SaveNotification #Id = #tempId;
END TRY
BEGIN CATCH
-- some catch code
END CATCH
It should work:
CREATE PROCEDURE my_proc
#id UNIQUEIDENTIFIER
AS
SELECT #id;
GO
DECLARE #data XML =
'<metaData><item key="Id" value="903e9859-f8fd-4163-9303-b43f89fe977f"/>
</metaData>';
DECLARE #temp_Id UNIQUEIDENTIFIER
= #data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier');
EXEC my_proc #id = #temp_id;
LiveDemo
I suspect you have GO betweens calls and variable is not visible between batches like:
DECLARE #data XML =
'<metaData>
<item key="Id" value="903e9859-f8fd-4163-9303-b43f89fe977f"/>
</metaData>';
DECLARE #temp_Id UNIQUEIDENTIFIER
= #data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier');
GO
EXEC my_proc #id = #temp_id;
Error(s), warning(s):
Must declare the scalar variable "#temp_id".
LiveDemo2
Or you have nested calls and variable is out of scope.
EDIT:
Typo #temp_id <> #tempId:
DECLARE #temp_Id UNIQUEIDENTIFIER =
#data.value('/metaData[1]/item[#key="Id"][1]/#value', 'uniqueidentifier')
EXEC dbo.SaveNotification #Id = #tempId;
Working code
You've mixed two naming conventions. The key point is to be consistent. It can save a lot of headaches.
Since a week am working with MYSQL , got to execute the Stored Procedure as well as Views but Facing some problem retrieving the values returned from a function.
Here's the Function:
CREATE DEFINER=`root`#`localhost` FUNCTION `GetProductIdsStringByEnquiryId`
(
InEnquiryId int
) RETURNS varchar(4000) CHARSET utf8
BEGIN
DECLARE InProductIds varchar(4000);
DECLARE ProductId varchar(50);
DECLARE x,y,z INT;
DECLARE sp1_cursor CURSOR FOR SELECT ProductId FROM enquiryproductid where
EnquiryId=InEnquiryId;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET z = 1;
SET InProductIds='';
OPEN sp1_cursor;
REPEAT
FETCH sp1_cursor INTO ProductId;
SETInProductIds=concat(InProductIds,ProductId,',');
UNTIL (z=1)
END REPEAT;
CLOSE sp1_cursor;
RETURN InProductIds ;
END
I was initially working with SQL SERVER 2005, and the function which I have written in their I tried converting it as above in MYSQL,
Here's the SQL Function Code:
CREATE function [dbo].[GetBranchIdsStringByEmployeeId]
(
#EmployeeId as integer
)
returns nvarchar(4000)
as
begin
declare #BranchIds as nvarchar(4000)
set #BranchIds=''
if exists(select 1 from dbo.BranchEmployees where EmployeeId=#EmployeeId)
begin
select #BranchIds=#BranchIds+cast(BranchId as nvarchar(50))
+',' from dbo.BranchEmployees where EmployeeId=#EmployeeId
order by BranchId
end
return #BranchIds
end
Can anybody Please Let me know if the Function What I have written in MYSQL is in ProperManner or not? Please do help me out.
Thank You.
Not read fully through it, but few comments
Variable assignment in mysql uses := (in set #variable it is ok to use =, in select #variable:=#variable+1)
Are you trying to
SELECT group_concat(BranchId)
FROM dbo.BranchEmployees
WHERE EmployeeId = #EmployeeId
?