Using Case statement within update clause - Sybase - sql

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" ?

Related

DB2 Query Token SMALLINT was not valid

Forum.
I am working with IBM System i Version 7.1.
I am having issues in the source code with the following merge statement so I copied it over to the database client to utilize the "Run SQL Scripts" functionality.
Rather than replacing the coded in #Variables in the statement I wanted to declare local variables so that I could test the statement as is.
The added the following 'declare and set' lines and I get the following error:
declare #groupId smallint
set #groupId = 99
declare #groupName varchar(40)
set #groupName = 'Sam'
declare #groupId smallint
SQL State: 42601
Vendor Code: -104
Message: [SQL0104] Token SMALLINT was not valid. Valid tokens: DYNAMIC SENSITIVE ASENSITIVE INSENSITIVE. Cause . . . . . : A syntax
error was detected at token SMALLINT. Token SMALLINT is not a valid
token. A partial list of valid tokens is DYNAMIC SENSITIVE ASENSITIVE
INSENSITIVE. This list assumes that the statement is correct up to
the token. The error may be earlier in the statement, but the syntax
of the statement appears to be valid up to this point. Recovery . . .
: Do one or more of the following and try the request again: --
Verify the SQL statement in the area of the token SMALLINT. Correct
the statement. The error could be a missing comma or quotation mark,
it could be a misspelled word, or it could be related to the order of
clauses. -- If the error token is , correct the SQL
statement because it does not end with a valid clause.
Processing ended because the highlighted statement did not complete >successfully
I have tried adding semicolons to the end of each line and begin and end statements and still no success.
Below is the whole statement I am trying to execute:
declare #groupId smallint
set #groupId = 99
declare #groupName varchar(40)
set #groupName = 'Sam'
merge into database.table as t
using ( values( cast(#groupId as smallint)
,cast(#groupName as varchar(40))
))
as caz( group_id
, group_name
)
on t.group_id = caz.group_id
when matched then update
set t.group_name = caz.group_name
when not matched then
insert ( group_id
, group_name
)
values (caz.group_id
, caz.group_name
);
Any help is appreciated.
Please let me know if I may provide anymore information.
I may have found an answer here: https://stackoverflow.com/a/4451159/2272357
CREATE OR REPLACE VARIABLE variableName VARCHAR(50);
SET variableName = 'blah';
SELECT * FROM table WHERE column = variableName;
DROP VARIABLE variableName;
I have yet to verify it works successfully. I believe it may be a local issue though.

SQL If not Null

Excuse my attempt at this, very rusty with SQL.
When I run the following code:
"IF NOT ISNULL Then INSERT INTO [XX].[dbo].[XXX] end if(
I get the following error message "AN EXPRESSION OF NON-BOOLEAN TYPE SPECIFIED"
I have tried to find out how to solve this error with no luck.
Basically I want to insert into [xx] if the cell is 'NOT NULL'.
you're missing the cell value to compare:
try this:
IF NOT ISNULL <cellValue> Then INSERT INTO [XX].[dbo].[XXX] end if ...
Since you refer to "dbo" I guess you're working with Sybase or SQL Server. It looks like you want to insert a single row of values based on a variable at hand. The accepted answer may work in your specific case but I think the more generic solution is below:
insert into <table> (<column list>)
select <values to insert>
where <#variable or column_value> is not null
The accepted answer has syntax that is not valid for Sybase/SQL Server. If you prefer that approach it should look something like the below:
if <cellvalue> is not null begin insert into <table> (...) values (...) end

Must declare the scalar variable with SELECT statement

I have the following statement:
DECLARE #Nr_Karton int;
SELECT #Nr_Karton = ISNULL(MAX(Nr), 1000) FROM W_Karton;
SET #Nr_Karton = #Nr_Karton + 1;
INSERT INTO W_Karton (Container_ID, Nr, Beschrieb, CreationDate, Location)
VALUES ('1', #Nr_Karton, '', getDate(), 'Bösingen');
But I get the error:
[SQL] SELECT #Nr_Karton = ISNULL(MAX(Nr), 1000) FROM W_Karton
[Err] 42000 - [SQL Server]Must declare the scalar variable "#Nr_Karton".
How to get rid of the error?
I did some playing with this. The fictional schema I created was:
CREATE TABLE W_Karton (Container_ID int, Nr int, Beschrieb varchar(1),
CreationDate datetime, Location varchar(10))
Whilst it parsed and ran fine on my local 2008R2 box, the same code did not work when pasted into a SQL Fiddle.
However, if you remove all the semi-colons apart from the last one as per this SQL Fiddle you can see it seems to work fine!
I believe it shouldn't make any difference, but if you would rather it worked and don't care about the why, give it a try...
I encountered the same issue. It turns out it is due to ';' being selected as the "Query Terminator". IN SQL Fiddle, this actually means "batch terminator". There should be a drop-down button on the bottom right that has the text "[;]". Click that and select "Keyword [GO]".

Handling blank query results, ISNULL(NULLIF())

Im trying to return the results of a query into a variable to I am able to use it in a insert statement. However my select keeps returning null even though I believe I have the ISNULL(NULLIF()) set up correctly.
declare #test varchar(10)
set #test = (select ISNULL(NULLIF(discnumber,''),'NA')
from discmaster
where meteredfilename = '3501' or nonmeteredfilename='3501')
select #test
It seems correct but still returns a NULL value. Would you Select Case but not 100% sure on that method either.
I'd move the ISNULL outside of the subquery - what if the subquery returns no results?
set #test = COALESCE(
(select NULLIF(discnumber,'')
from discmaster
where meteredfilename = '3501' or nonmeteredfilename='3501')
,'NA')
(I've also been a snob and replaced ISNULL with COALESCE. Outside of a few scenarios, COAELSCE is the better function).
Instead of ISNULL(NULLIF(discnumber,''),'NA')
try COALESCE(discnumber,'NA')

Conditional IF in T-SQL Errors?

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