This question must be really silly, but I haven't found an answer for it yet.
I'm making a program in C # that dynamically writes a script to run on SQL Server. I declared two variables that receive the values returned from two calls exec 'procedure_name'.
In the next block of the script, I want these variables to be set to zero.
How to do this using a SET?
would be something like this:
SET # a, # b = 0?
You can do it via SELECT:
SELECT #a = 0, #b = 0
With SET you need 2 SET commands:
SET #a = 0; SET #b = 0
Method 1
set #a = 0
set #b = 0
Method 2
Select #a = 0, #b = 0
Select #a = 0
select #b = 0
OR
Set #a=0
Set #b= #a
Or
set #a = 0
set #b= 0
Related
There are two cases, one of which passes while other fails.
Case 1: Fails or Returns 0 for all #{password} values
SELECT #out = CASE WHEN (SELECT CONVERT( VARCHAR(MAX), DECRYPTBYKEY(hashed_key)) AS someKey FROM t_key_table WHERE is_active = 1) = '#{password}'
THEN 1
ELSE 0
END
Case 2: Passes on correct ${password} value and returns 0 for others
SELECT #out = CASE WHEN (SELECT CONVERT( VARCHAR(MAX), DECRYPTBYKEY(hashed_key)) AS someKey FROM t_key_table WHERE is_active = 1) = '${password}'
THEN 1
ELSE 0
END
Currently my reasoning is that #{} returns PreparedStatement which might hinder with the comparison process, but i am unable to find any specific doc or something concrete to help me understand this. Any help is appreciated.
IF(
[dbo.tblx.Category] = 'WS' OR [dbo.tblx.Category] = 'SEM',
0,
[dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
)
this returns the entire text and not the "calculation"
can someone help me...what am I doing wrong?
i tried
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000
END AS Metal
Do perhaps insert something before "CASE WHEN"? it is not running
As I understood, you are trying to create a computed column, using value from other table.
I think in this case you have to use a User Defined function in the formula for computed column. If you give more information I can try to write some code for you.
Instead, if you just want to add a column in a query you can use:
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblPTx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000 END AS NAME_OF_YOUR_NEW_COLUMN
Update
DECLARE #myvar INT;
SET #myvar = CASE WHEN ([dbo.tblx.Category] = WS OR [dbo.tblPTx.Category] = SEM) THEN 0
ELSE [dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
END
I have a store procedure where I pass a path to the file like:
EXEC spMyPathFile
#PFile = 'C:\TFiles\Paths\Test_1.1_Version.txt'
What I'd like to do it loop through and be able to pass a number of versions of the file like 1.1 and 1.2 etc using:
DECLARE #intLp INT
DECLARE #a varchar(2)
SET #intLp = 1 WHILE (#intLp <2)
BEGIN IF #intLp = 1 BEGIN
SET #a = '1.1'
END
ELSE IF #intLp = 2
BEGIN
SET #a = '1.2'
END
EXEC spMyPathFile
#PFile = 'C:\TFiles\Paths\Test_'+#a+'_Version.txt'
SET #intLp = #intLp + 1
END
For some reason I get "Incorrect syntax near '+'." which is just before the #a. I'm obviously not joining my variable to my string properly.
Could someone give me an example of how this should look?
Change
EXEC spMyPathFile
#PFile = 'C:\TFiles\Paths\Test_'+#a+'_Version.txt'
to
declare #FileName varchar(100) = 'C:\TFiles\Paths\Test_' + #a + '_Version.txt'
EXEC spMyPathFile
#PFile = #FileName
Edit:
From MSDN - Specify Parameters
The parameter values supplied with a procedure call must be constants or a variable; a function name cannot be used as a parameter value. Variables can be user-defined or system variables such as ##spid.
I've got to update a column value by decreasing the value in the column by a variable.
There are two conditions:
1. where the row count = 1
2. where the row count is more than 1
I've got it set to do the single row count but need help when the query returns multiple rows.
set #rowsCounted = (select COUNT(QuantityA) from Offers where WID = #wId and ND = #nd)
if(#rowsCounted = 1)
begin
set #QuantityAvailable = (select QuantityA from Offers where WID = #wId and ND = #nd)
set #QuantityAvailable = (select #QuantityAvailable - #QuantityAdjusted)
update Offers
set QuantityA = #QuantityAvailable
where WID = #wId and ND = #nd
end
else
begin
select #rowsCounted as rowsCounted -- example of 4 rows with values of = 287,280,288,288
--begin loop as the QuantityA may contain different values
end
If #QuantityAdjusted is constant for the procedure, then you only need one update statement. Use set-based thought constructs rather than procedural-based ones:
update Offers
set QuantityA = QuantityA - #QuantityAdjusted
where WID = #wId and ND = #nd
This will update in a set-based operation, and there is no need to construct your own loop. This is part of what SQL engines are meant to do.
I am writing a stored proc that calculates a WHOLE bunch of different things, but I have a bit in it, that is repeated about 9 times.
eg:
if #argA = 1 (true)
select Count(samples) from dbo.X where type = #argAType
if #argB = 1 (true)
select Count(samples) from dbo.X where type = #argBType
if #argC = 1
select Count(samples) from dbo.X where type = #argCType
and so on...
how can I write a function (or something similar) that I can pass in a bit (true or false), and other argument, and only return the result set if true???
Is this what you're looking for? This is the best I can deduce based on the question as it's currently posted.
SELECT COUNT(samples)
FROM dbo.X
WHERE
(type=#argAType AND #argA=1)
OR
(type=#argBType AND #argB=1)
OR
(type=#argCType AND #argC=1)
In function form, I think this is right:
CREATE FUNCTION GetCount(#n AS BIGINT) RETURNS BIGINT
AS
BEGIN
DECLARE #count BIGINT
SELECT #count = COUNT(samples)
FROM dbo.X
WHERE
(type=#argAType AND #argA=1)
OR
(type=#argBType AND #argB=1)
OR
(type=#argCType AND #argC=1)
RETURN #count
END