I have this stored procdure
CREATE PROC dob.p_foobar
(
#foo bit = NULL,
#Bar bit = NULL
)
AS
IF #Foo == 1
BEGIN
SELECT 'Hello, World'
END
When I parse, I get the error "Incorrect syntax near '='".
Please tell me what I'm doing wrong. I know it's something stupid, but I just can't figure it out.
Thank you
SQL uses a single equals sign (=), not a double equals sign (==)
No ==
IF #Foo = 1
BEGIN
...
END
Related
I've looked into this and I'm just struggling to accept that it isn't possible to do.
I write queries for others to use and normally include some declared variables at the top so the user can "filter" their search at the top of the query and not bother with the rest.
Sometimes the user may not want to specify the values for a particular variable, as they want to return all types. I know this isn't what SQL is for and that there are reporting tools. Any suggestions on how to do this?
DECLARE #foo int, #bar bit
SET #foo = *
SET #bar = *
SELECT *
FROM table
WHERE foo = #foo
AND bar = #bar
* isn't a valid int value and even if an int value could be used as a wildcard, you would need to use LIKE not =.
What you want here is a NULL value and the proper boolean logic.
SELECT {Columns}
FROM [Table]
WHERE (foo = #foo OR #foo IS NULL)
AND (bar = #bar OR #bar IS NULL)
OPTION (RECOMPILE);-- Comment out/remove if you aren't using T-sQL due to incorrect tagging
I have a Procedure which returns MobileAreaCode+Mobile if they are not null
I just want to add to it support for empty strings as well I tried(without the handle for empty string it works)
ALTER PROCEDURE PROC_NAME
#Identification INT
AS
BEGIN
SELECT
CASE WHEN MobileAreaCode is NOT NULL OR Mobile is NOT NULL
OR MobileAreaCode<>'' OR Mobile<>''
THEN
MobileAreaCode+Mobile
END
FROM
TABLE_NAME
WHERE
id = 123456789
END
GO
which doesn't work and results the following error:
Incorrect syntax near the keyword 'FROM'.
You can use Use NULLIF (Transact-SQL).
select nullif(MobileAreaCode, '')+nullif(Mobile, '') as MobileAreaCodeMobile
from YourTable
SQL Fiddle
ALTER PROCEDURE PROC_NAME
#Identification INT
AS
BEGIN
SELECT
CASE WHEN (MobileAreaCode is NOT NULL) AND (Mobile is NOT NULL)
AND (len(MobileAreaCode)>0) AND (len(Mobile)>0)
THEN
MobileAreaCode+Mobile
END
FROM
TABLE_NAME
WHERE
id = 123456789
END
GO
The following is sybase code. Can someone see if the following is correct. I guess I'm missing out on the syntax somewhere
declare #test varchar(32)
select #test="/data/dump/team/"
update link
set link.value=
case when #test=substring(link.value,1,17)
then #test
when #test != substring(link.value,1,17)
value
end
where link.value != ""
and link_id=0 and row_id = 462135
As it is give me the following error: "Incorrect syntax near keyword end on line 10."
Can please someone help me with the syntax.
Try adding "then" to the second case:
declare #test varchar(32)
select #test="/data/dump/team/"
update link
set link.value=
case when #test=substring(link.value,1,17)
then #test
when #test != substring(link.value,1,17)
then value
end
Why not simply do an "else" for the second "when" ?
I have this XML Query in SQL Server 2005:
SElECT XmlField FROM tablename WHERE xmlField.exist('(/Root/Name[id="10")[1]') = 1
However, I want to replace the value "10" with a parameter that I pass to the Stored Procedure. How do I achieve this? I have tried using "#variablename" but it doesn't work.
Thanks in advance.
Probably, you want to have something like
SELECT XmlField FROM tablename WHERE xmlField.exist('(/Root/Name[id="{ sql:variable("#variablename") }")[1]') = 1
See http://msdn.microsoft.com/en-us/library/ms188254(v=SQL.100).aspx for how to access variables and columns in XQuery in SQL Server.
After a few minutes of hair pulling...i found an answer...
Result_XML.exist('(/Root/Name[id="{sql:variable("#myId")}"])[1]') = 1
should be written as
Result_XML.exist('(/Root/Name[id=(sql:variable("#myId"))])[1]') = 1
I replaced the "{ and }" with ( and ) to enclose the sql:variable keyword.
There is one more thing I found out about by many many trials: if your variable is a char value, if you declare it in your sql statement, it should be varchar, not char.
This sql didn't return any results:
DECLARE #myparam char(50)
SET #myparam = 'someval'
...
WHERE
t.c.exist('/root/child[text() = sql:variable("#myparam ")]') = 1
But this did:
DECLARE #myparam varchar(50)
SET #myparam = 'someval'
...
WHERE
t.c.exist('/root/child[text() = sql:variable("#myparam ")]') = 1
Maybe this is obvious, but I spent some time before I figured the reason why no records would be returned.
This question could easily take multiple paths, so I will hit the more specific path first. While working with SQL Server 2005, I'm trying to create a scalar function that acts as a 'TryCast' from varchar to int. Where I encounter a problem is when I add a TRY block in the function;
CREATE FUNCTION u_TryCastInt
(
#Value as VARCHAR(MAX)
)
RETURNS Int
AS
BEGIN
DECLARE #Output AS Int
BEGIN TRY
SET #Output = CONVERT(Int, #Value)
END TRY
BEGIN CATCH
SET #Output = 0
END CATCH
RETURN #Output
END
Turns out theres all sorts of things wrong with this statement including "Invalid use of side-effecting or time-dependent operator in 'BEGIN TRY' within a function" and "Invalid use of side-effecting or time-dependent operator in 'END TRY' within a function". I can't seem to find any examples of using try statements within a scalar function, which got me thinking, is error handling in a function is possible?
The goal here is to make a robust version of the Convert or Cast functions to allow a SELECT statement carry through depsite conversion errors. For example, take the following;
CREATE TABLE tblTest
(
f1 VARCHAR(50)
)
GO
INSERT INTO tblTest(f1) VALUES('1')
INSERT INTO tblTest(f1) VALUES('2')
INSERT INTO tblTest(f1) VALUES('3')
INSERT INTO tblTest(f1) VALUES('f')
INSERT INTO tblTest(f1) VALUES('5')
INSERT INTO tblTest(f1) VALUES('1.1')
SELECT CONVERT(int,f1) AS f1_num FROM tblTest
DROP TABLE tblTest
It never reaches point of dropping the table because the execution gets hung on trying to convert 'f' to an integer. I want to be able to do something like this;
SELECT u_TryCastInt(f1) AS f1_num FROM tblTest
fi_num
__________
1
2
3
0
5
0
Any thoughts on this? Is there anything that exists that handles this? Also, I would like to try and expand the conversation to support SQL Server 2000 since Try blocks are not an option in that scenario.
Check if you can convert to int first, check out the IsInteger function here: IsNumeric, IsInt, IsNumber It will work on 2000 and up
And, to answer in general: No, you can't use try-catch logic in a function. I can sort of see why - or at least it's clearly preferable to avoid it, given the huge performance penalty that would come with it.
However, I think it is odd that one also cannot RAISE an error in a function... that's something built-in functions already do. I suppose one has to get by returning NULL.
The TRY…CATCH construct cannot be used in a user-defined function in SQL 2012!
See this:
http://msdn.microsoft.com/en-us/library/ms175976.aspx
When I try to use this script:
CREATE FUNCTION u_TryCastInt
(
#Value as VARCHAR(MAX)
)
RETURNS Int
AS
BEGIN
DECLARE #Output AS Int
BEGIN TRY
SET #Output = CONVERT(Int, #Value)
END TRY
BEGIN CATCH
SET #Output = 0
END CATCH
RETURN #Output
END
I got error:
Msg 443, Level 16, State 14, Procedure u_TryCastInt, Line 10
Invalid use of a side-effecting operator 'BEGIN TRY' within a function.
Msg 443, Level 16, State 14, Procedure u_TryCastInt, Line 12
Invalid use of a side-effecting operator 'END TRY' within a function.
Msg 443, Level 16, State 14, Procedure u_TryCastInt, Line 13
Invalid use of a side-effecting operator 'BEGIN CATCH' within a function.
Msg 443, Level 16, State 14, Procedure u_TryCastInt, Line 15
Invalid use of a side-effecting operator 'END CATCH' within a function.