How to set value of multiple variable in T-SQL? - sql-server

How to set value of multiple variable in SQL Server 2008. Like
Set #a = 10, #b = 'James'

Besides using SELECT #a = 10, #b = 'James', if you just want to set the values the first time they are declared, you can set the values of multiple variables when declaring them, as shown below:
DECLARE #a INT = 10, #b VARCHAR(10) = 'James' ;

Use SELECT instead of SET:
SELECT #a = 10, #b = 'James';

SELECT #a = 10, #b = 'James'
This should work, if the variables are properly declared.

Try like this,
Declare #a Int,#b Varchar(10)
select #a=10,#b='James'

As follows:
SET #a=10
SET #b='James'

Related

dynamically change variable value in tsql

This script
declare #a varchar(10), #b varchar(44)
set #b = 'abc'
set #a = #b
print #a
set #b = 'xyz'
--without doing this set #a = #b here
print #a
prints this :
abc
abc
Is it possible to print this somehow?
abc
xyz
Basically, I don't want to reassign #a explicitly. Maybe there is a function or something which will reassign #a? Something like REPROCESS(#a). I just don't want to reassign RHS each time, because it is a huge script on my side, just want to save some space
Actual script on my side looks like #a = 'blahblahblha 1000 lines more' + #b + 'another 1000 lines'. And then I use #a, and then I change #b to some other value and then I want to use #a again with updated #b
It sounds like you really want to build a template with a placeholder and then build the final string based on the template with different values for the placeholder. So something like this (where '{b placeholder}' is any literal string that you're sure won't appear in the final string you're building).
set #template_a =
'1000 lines ' +
' {b placeholder} ' +
'1000 lines';
set #b = 'Value 1';
set #a = replace( #template_a, '{b placeholder}', #b );
-- do something with #a
set #b = 'Value 2';
set #a = replace( #template_a, '{b placeholder}', #b );
-- do something with #a
Of course, you could factor out the set/ replace/ do something logic into a separate procedure that your script calls so you're not copying and pasting so much code.
declare #a varchar(10), #b varchar(44)
set #b = 'abc'
set #a = 'xyz'
print 'a actual value' print #a
print 'b actual value' print #b
SET #b = substring(#a+#b,1, LEN(#a+#b) - LEN(#b));
SET #a =substring(#a+#b,LEN(#b)+1,LEN(#a+#b));
print '------Swipe Result-------'
print 'a value' print #a
print 'b value' print #b

T-SQL stored procedure - Detecting if a parameter is supplied as OUTPUT

Consider the following T-SQL code snippet:
CREATE PROC dbo.SquareNum(#i INT OUTPUT)
AS
BEGIN
SET #i = #i * #i
--SELECT #i
END
GO
DECLARE #a INT = 3, #b INT = 5
EXEC dbo.SquareNum #a OUTPUT
EXEC dbo.SquareNum #b
SELECT #a AS ASQUARE, #b AS BSQUARE
GO
DROP PROC dbo.SquareNum
The result set is:
ASQUARE BSQUARE
----------- -----------
9 5
As can be seen, #b is not squared, b/c it was not passed-in as output parameter (no OUTPUT qualifier when passing in the parameter).
I would like to know if there is a way I could check within stored procedure body (dbo.SquareNum body in this case) to see if a parameter has indeed been passed in as an OUTPUT parameter?
------ THIS WILL GIVE YOU THE BOTH VALUE IN squared------
CREATE PROC dbo.SquareNum(#i INT OUTPUT)
AS
BEGIN
SET #i = #i * #i
--SELECT #i
END
GO
DECLARE #a INT = 3, #b INT = 5
EXEC dbo.SquareNum #a OUTPUT
EXEC dbo.SquareNum #b OUTPUT
SELECT #a AS ASQUARE, #b AS BSQUARE
GO
DROP PROC dbo.SquareNum
-----TO CHECK STORED PROCEDURE BODY-----
SELECT OBJECT_NAME(object_id),
OBJECT_DEFINITION(object_id)
FROM sys.procedures
WHERE OBJECT_DEFINITION(object_id) =(SP_NAME)
Actually, there is a very simple way!
Make the parameter optional by setting a default value (#Qty AS Money = 0 Below)
Then, pass a value OTHER THAN THE DEFAULT when calling the procedure. Then immediately test the value and if it is other than the default value you know the variable has been passed.
Create Procedure MyProcedure(#PN AS NVarchar(50), #Rev AS NVarchar(5), #Qty AS Money = 0 OUTPUT) AS BEGIN
DECLARE #QtyPassed AS Bit = 0
IF #Qty <> 0 SET #QtyPassed = 1
Of course that means the variable cannot be used for anything other than OUTPUT unless you have a default value that you know will never be used as an INPUT value.
You can do this by query to sys views:
select
p.name as proc_name,
par.name as parameter_name,
par.is_output
from sys.procedures p
inner join sys.parameters par on par.object_id=p.object_id
where p.name = 'SquareNum'
or check in Management Studio in database tree:
[database] -> Programmability -> Stored Procedures -> [procedure] -> Parameters
Maybe I'm wrong but I don't believe it's possible. OUTPUT is part of the stored procedure definition so you should know when a parameter is or not OUTPUT. There is no way to set it dynamically so I think it's pointless to determine by code when a parameter is output or not because you already know it.
If you are trying to write a dynamic code, Piotr Lasota's answer should drive you to the correct way to realize when a parameter is Output.
Use the following query to get the name of all the parameters and to check if it is a output parameter:
select name, is_output from sys.parameters

Perform a Sql Server Spatial Intersection against a table full of spatial features?

I have a table in sql 2015 that has a geometry column and about 10,000 records. I want to test if a new record i am thinking about adding overlaps any of the existing records.
I know that i can compare two features with STIntersects like this:
DECLARE #a geometry;
DECLARE #b geometry;
SET #a = GEOMETRY::STPolyFromText('POLYGON((-10277454.3014
4527261.7601, -10277449.1674 4527236.5722, -10277503.1433 4527245.177, -10277462.2333 4527281.9267, -10277454.3014 4527261.7601))',3857);
SELECT #b = [Shape] FROM [GIS].[ggon].[blah] WHERE
OBJECTID = 4539;
SELECT #a.STIntersects (#b);
But what if i wanted to compare #a to ALL of the features in the blah table?
If the result of the intersection above is 1 then i know that the #a geometry intersects the #b geometry which it does. That works.
But if i change #b to be:
SELECT #b = [Shape] FROM [GIS].[ggon].[blah]
then i get a 0 - which is not correct
SELECT #a.STIntersects (SELECT [Shape] FROM [GIS_PL].[talon].[MDC_WM]);
also fails
DECLARE #a geometry; DECLARE #b geometry;
SET #a = GEOMETRY::STPolyFromText('POLYGON((-10277454.3014 4527261.7601, -10277449.1674 4527236.5722, -10277503.1433 4527245.177, -10277462.2333 4527281.9267, -10277454.3014 4527261.7601))',3857);
SELECT * FROM [GIS].[ggon].[blah] WHERE [Shape].STIntersects(#a) = 1

SQL Server 2008: Finding a value from a string

This might be a very simple select statement, here's my question.
I have a variable
DECLARE #A varchar(128) --declaring variable
SET #A = 'sus_123456_R5_20140506' --setting value
I want to find the value after 'sus_' and before 'R5'
Also, the value in between is not of fixed length. So the function has to be dynamic.
However it always have sus and _R5_date. That's constant.
SET #A = 'sus_129_R5_20150408
Thanks
Use SUBSTRING in combination with CHARINDEX:
SELECT SUBSTRING(#A,CHARINDEX('sus_',#A,0)+4,CHARINDEX('_R5',#A,0)-5)
You can try this once using SUBSTRING() and CHARINDEX() FUNCTION
DECLARE #A varchar(128) --declaring variable
SET #A = 'sus_123456_R5_20140506'
select substring(#A,charindex('_',#A)+1,charindex('r5',#A)- (2+charindex('_',#A)))
Results in: 123456
(OR) This one using LEFT(),RIGHT() and CHARINDEX() function.
DECLARE #A varchar(128)
SET #A = 'sus_129_R5_20150408'
select right(left(#A,charindex('R5',#A) - 2),charindex('_',#A) - 1)
Results in: 129
Read more about String Function

SQL Server - Compare int values in a select clause

This is what I thought would be a simple select clause, however the following is giving me grief! I am using SQL Server 2008.
Basically I want to compare two integer values and return the boolean result in the select clause. Here is a simple example:
DECLARE #A INT
DECLARE #B INT
SET #A = 1
SET #B = 2
SELECT #A = #B
Currently the only output is "Command(s) completed successfully."
Where I reasonably believe it is assigning #A to #B.
I thought this would be simple but have not been able to achieve this.
Any help would be great! Thanks
try
SELECT CASE WHEN #A = #B THEN 1 ELSE 0 END
instead
Select Case When #A = #B Then 1 Else 0 End
Where I reasonably believe it is assigning #A to #B.
No... it is assigning B to A.
You need this:
SELECT case when #A - #B = 0 then 1 else 0 end

Resources