I have a function whose return value I am assigning it to a variable and I am getting an error
Incorrect syntax near the keyword 'TOP'. SQL Server
if #Miracle is null OR #Miracle =''
select #Miracle = TOP(1) M.MiracleName
FROM Miracle M where M.MiracelID = #MiracelID
how can I assign functions like TOP to a variable ??
I think you just need to move TOP 1 before your variable:
select TOP 1 #Miracle = M.MiracleName
...
Your aren't assigning TOP to a variable, but rather using TOP to tell SQL Server to only return a single row.
Good luck.
It is simple as this.
if #Miracle is null OR #Miracle =''
select TOP 1 #Miracle = M.MiracleName
FROM Miracle M where M.MiracelID = #MiracelID
Related
I'm using MS SQL Server 2019
I have a string in a table (tblJobCosts) that has its own ID like this:
TextID jobText
1 Total Cost for job is £[]. This includes VAT
How do I update the value stored in the brackets based on the value from another table?
The end result would look like this:
Total Cost for job is £500. This includes VAT
I thought I could incorporate a SELECT with a REPLACE but this does not seem possible:
DECLARE #JobNum INT = 123;
UPDATE dbo.JobCosts
SET jobText = REPLACE (jobText,'[]',
SELECT JH.jobCost
FROM dbo.JobHead AS JH
WHERE (JH.JobNo = #JobNum)
) AND TextID = 1
If I run the above I receive the error:
Incorrect syntax near the keyword 'SELECT'.
Is it possible to incorporate a SELECT with a REPLACE?
I think that you cannot call a select statement in the replace function.
I would try something like that:
UPDATE dbo.JobCosts
SET jobText = REPLACE (jobText,'[]',k.the_cost) from
( SELECT JH.jobCost as the_cost
FROM dbo.JobHead AS JH
WHERE (JH.JobNo = #JobNum)
)k
where TextID = 1
I declare some filters on a form and pass them into my SP in SQL for running a select command and this is how I did that, but it didn't work:
DECLARE #Confirm_Filter AS BIT=NULL,
#ReciveDate_Filter AS NCHAR(10) = NULL
....
select * from .......
where
(#Confirm_Filter IS NOT NULL AND InterviewConfirm = #Confirm_Filter)
AND
(#ReciveDate_Filter IS NOT NULL AND ReciveDate BETWEEN GETDATE() AND #ReciveDate_Filter)
and in the where section I want to do:
where
if #Confirm_Filter IS NOT NULL Then (Select * from ... where InterviewConfirm = #Confirm_Filter)
AND
if #ReciveDate_Filter IS NOT NULL Then (select * from .... where ReciveDate BETWEEN GETDATE() AND #ReciveDate_Filter)
and I know above form is totally mistake but how could I do it?
Your problem is called dynamic search conditions. It is solved either by using dynamic SQL, or the following pattern (which is similar to your first version, but with a few changes):
select * from .......
where
(#Confirm_Filter IS NULL OR InterviewConfirm = #Confirm_Filter)
AND
(#ReciveDate_Filter IS NULL OR ReciveDate BETWEEN GETDATE() AND #ReciveDate_Filter)
I am translating a query from PostgreSQL to SQL Server. I didn't write the query in PostgreSQL and it's quite complicated for my knowledge so i don't understand every piece of it.
From my understand: we are trying to find the max version from p_policy and when insurancestatus = 7 or 14 / transactiontype = CAN, we compare two dates (whose format are BIG INT).
This is the PG Query:
SELECT *
FROM BLABLABLA
WHERE
pol.vnumber = (
SELECT MAX(pol1.vnumber)
FROM p_policy pol1
AND ( CASE WHEN pol1.insurancestatus IN (7,14)
or pol1.transactiontype IN ('CAN')
-- ('CAN','RCA')
THEN pol1.veffectivedate = pol1.vexpirydate
ELSE pol1.veffectivedate <> pol1.vexpirydate
END
)
AND pol1.vrecordstatus NOT IN (30,254)
etc.
I am used to have a where statement where I compare it to a value. I understand here from the Case statement we will have a boolean, but still that must be compared to something?
Anyway the main purpose is to make it work in SQL, but I believe SQL can't read a CASE statement where THEN is a comparison.
This is what I tried:
SELECT *
FROM BLABLABLA
WHERE pol.vnumber =
(
SELECT MAX(pol1.vnumber)
FROM p_policy pol1
WHERE sbuid = 4019
AND ( CASE WHEN pol1.insurancestatus IN (7,14)
or pol1.transactiontype IN ('CAN')
THEN CASE
WHEN pol1.veffectivedate = pol1.vexpirydate THEN 1
WHEN pol1.veffectivedate <> pol1.vexpirydate THEN 0
END
END
)
AND pol1.vrecordstatus NOT IN (30,254)
etc.
And then I get this error from SQL Server (which directly the last line of the current code - so after the double case statement)
Msg 4145, Level 15, State 1, Line 55
An expression of non-boolean type specified in a context where a condition is expected, near 'AND'.
Thank you !Let me know if it is not clear
I think you want boolean logic. The CASE expression would translate as:
(
(
(pol1.insurancestatus IN (7,14) OR pol1.transactiontype = 'CAN')
AND pol1.veffectivedate = pol1.vexpirydate
) OR (
NOT (pol1.insurancestatus IN (7,14) OR pol1.transactiontype = 'CAN')
AND pol1.veffectivedate <> pol1.vexpirydate
)
)
There are 2 main issues with your snippet, SQL Server-syntax-wise.
SELECT * FROM BLABLABLA WHERE
pol.vnumber = /* PROBLEM 1: we haven't defined pol yet; SQL Server has no idea what pol.vnumber is here, so you're going to get an error when you resolve your boolean issue */
(
SELECT MAX(pol1.vnumber)
FROM p_policy pol1
WHERE sbuid = 4019
AND ( CASE WHEN pol1.insurancestatus IN (7,14)
or pol1.transactiontype IN ('CAN')
THEN CASE
WHEN pol1.veffectivedate = pol1.vexpirydate THEN 1
WHEN pol1.veffectivedate <> pol1.vexpirydate THEN 0
END
END
) /* PROBLEM 2: Your case statement returns a 1 or a 0..
which means your WHERE is saying
WHERE sbuid = 4019
AND (1)
AND pol1.vrecordstatus NOT IN (30,254)
SQL Doesn't like that. I think you meant to add a boolean operation using your 1 or 0 after the parenthesis.
like this: */
= 1
AND pol1.vrecordstatus NOT IN (30,254)
For testing in Toad, I have the following code
select ...
from ...
where term_code = :termcode AND
(
case
when :theSubject is not null then SUBJ_CODE = :theSubject
else 1 = 1
end
)
AND ptrm_code <> 8
In short: If theSubject is not entered (is null) I want to display all the courses, otherwise I want to display only those where subject_code is the same as the one entered in the variable window in Toad.
But I get an error:
[Error] Execution (77: 68): ORA-00905: missing keyword
in here:
when :theCourse is not null then sect.SSBSECT_SUBJ_CODE = theCourse
You can use boolean logic:
where
term_code = :termcode
and (:theSubject is null or subj_code = :theSubject)
I am relatively very new to SQL queries but I have this stored procedure where I am trying to get value of a declared variable as mentioned below but getting error,
First line is 20 here,
declare #m_ID_v int
set #m_ID_v = ( select ID_C from M_T where MName_C = #MName_parameter)
declare #g bit
if (select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable)
set #g_v = 1
else
set #g_variable = 0
Exception I get:
Msg 4145, Level 15, State 1, Procedure GetID, Line 20
An expression of non-boolean type specified in a context where a
condition is expected, near 'set'. Msg 156, Level 15, State 1,
Procedure GetID, Line 21 Incorrect syntax near the keyword 'else'.
Now if I remove declare #g... and try to parse it, no error occurs
EDIT
I want my code to check for returned value by my select statement so "if exists" is not really what am looking for, sorry.
try use if exists:
declare #g_v bit
if exists(select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable)
set #g_v = 1
else
set #g_v = 0
declare #m_ID_v int
set #m_ID_v = ( select ID_C from M_T where MName_C = #MName_parameter)
declare #g bit
if ((select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable) = value )
set #g_v = 1
else
set #g_variable = 0
You can't say
if (select ...
You have to compare something with something else in a if statement, or use a boolean function such as exists