SQL if then statement - sql

retry:
exec xxxxxxxxx
if #csvfilecount < 16
begin
waitfor delay '00:05:00'
goto retry
end
else
begin
send email
end
I like to use go to retry for 2 try only and then go to send email.
Not sure to how do this. Please help. Thanks

Can you use a while loop with a counter?
declare #counter as int
set #counter = 0
while #counter <= 2
begin
#counter = #counter + 1
-- your code here
-- update #csvfilecount
if #csvfilecount < 16
begin
waitfor delay '00:05:00'
end
else
begin
send email
-- do you want to BREAK here?
end
end

Related

Sybase looping not working - interactive sql

im trying the do a simple cursor that will give me data , executing the cursor in a 5 seconds interval .
but it only shows 1 row and stop... im working on sybase interactive sql , this is the query:
DECLARE #codigo INTEGER
DECLARE alarm_history_cur CURSOR FOR
SELECT AlarmHistoryID FROM DBA.AlarmHistory
WHERE DBA.AlarmHistory.Area = '1'
AND DBA.AlarmHistory.DateTimeOccurred < '2021-01-01 00:00:00.000'
OPEN alarm_history_cur
FETCH NEXT alarm_history_cur INTO #codigo
while ##sqlstatus = 0
BEGIN
WAITFOR DELAY '00:00:05'
SELECT * FROM DBA.AlarmHistory WHERE AlarmHistoryID = #codigo
END
BEGIN
FETCH NEXT alarm_history_cur
INTO #codigo
END
CLOSE alarm_history_cur
DEALLOCATE CURSOR alarm_history_cur
thanks in advance for further help.
Update :
im using sybase central - sql anywhere ..i will let some pictures to you
-##version pictureenter image description here
-original select
-what actually the syntax shows
enter image description here
enter image description here
The fetch command needs to be in the same begin/end pair associated with the while loop.
Instead of this:
while ##sqlstatus = 0
BEGIN
WAITFOR DELAY '00:00:05'
SELECT * FROM DBA.AlarmHistory WHERE AlarmHistoryID = #codigo
END
BEGIN
FETCH NEXT alarm_history_cur INTO #codigo
END
Try this:
while ##sqlstatus = 0
BEGIN
WAITFOR DELAY '00:00:05'
SELECT * FROM DBA.AlarmHistory WHERE AlarmHistoryID = #codigo
FETCH NEXT alarm_history_cur INTO #codigo
END

User defined function does work when passing variable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
The Camel scalar-valued function does not work when using variables
declare #primary_supervisor_name varchar(2000)
declare #lastname varchar(2000)
declare #containComma int
declare #position int
declare #result varchar(200)
DECLARE ts_cursor CURSOR FOR
select primary_supervisor_name from Test
OPEN ts_cursor
FETCH NEXT FROM ts_cursor INTO #primary_supervisor_name
WHILE ##FETCH_STATUS = 0
BEGIN
set #containComma = CHARINDEX(',', #primary_supervisor_name)
Begin
IF (#containComma = 0)
set #position = CHARINDEX(' ' , #primary_supervisor_name)
set #lastName = substring(#primary_supervisor_name, #position + 1, len(#primary_supervisor_name))
set #lastName = LOWER(#lastName)
print dbo.CamelCase('test') -- WORKS and PRINTs "Test"
print dbo.CamelCase(#lastName) -- does not do anything..no seeing any print output in console.
end
FETCH NEXT FROM ts_cursor INTO #primary_supervisor_name
END
CLOSE ts_cursor
DEALLOCATE ts_cursor
The camel function does not work for #lastName.
Using the Camel function from https://www.sqlservercentral.com/scripts/function-returns-string-in-proper-case-camelcase
I think the problem may be your program structure. Your BEGIN and END seem odd and off a bit.
WHILE ##FETCH_STATUS = 0
BEGIN
set #containComma = CHARINDEX(',', #primary_supervisor_name)
Begin
...
IF (#containComma = 0)
end
FETCH NEXT FROM ts_cursor INTO #primary_supervisor_name
END
I would think you were going for the code block below
WHILE ##FETCH_STATUS = 0
BEGIN
set #containComma = CHARINDEX(',', #primary_supervisor_name)
IF (#containComma = 0) BEGIN -- <--- Begin on this line?
...
end
FETCH NEXT FROM ts_cursor INTO #primary_supervisor_name
END

Algorithm for auto generated series number in sql

I want to make an algorithm for generate next series number by specified last series number in sql like below:
Last Number Next Number
> AAAA095 AAAA096
> AAAA999 AAAB001
> AAAB001 AAAB002
> AAAZ999 AABA001
After some try, Finally i got an algorithm & create it to SQL Scalar-valued function for this question as below
CREATE FUNCTION GetNextSeries ( #lastSeriesNo VARCHAR(8))
RETURNS VARCHAR(8)
AS
BEGIN
DECLARE #nextSeriesNo VARCHAR(8)
DECLARE #CHAR1 CHAR=SUBSTRING(#lastSeriesNo,1,1)
DECLARE #CHAR2 CHAR=SUBSTRING(#lastSeriesNo,2,1)
DECLARE #CHAR3 CHAR=SUBSTRING(#lastSeriesNo,3,1)
DECLARE #CHAR4 CHAR=SUBSTRING(#lastSeriesNo,4,1)
DECLARE #n INT=SUBSTRING(#lastSeriesNo,5,3)
SET #n = #n + 1
IF(#n>999)
BEGIN
SET #n=1
IF(#CHAR4<>'Z')
BEGIN
SET #CHAR4=CHAR(UNICODE(#CHAR4)+1)
END
ELSE IF(#CHAR3<>'Z')
BEGIN
SET #CHAR4='A'
SET #CHAR3=CHAR(UNICODE(#CHAR3)+1)
END
ELSE IF(#CHAR2<>'Z')
BEGIN
SET #CHAR4='A'
SET #CHAR3='A'
SET #CHAR2=CHAR(UNICODE(#CHAR2)+1)
END
ELSE IF(#CHAR1<>'Z')
BEGIN
SET #CHAR4='A'
SET #CHAR3='A'
SET #CHAR2='A'
SET #CHAR1=CHAR(UNICODE(#CHAR1)+1)
END
END
SET #nextSeriesNo=#CHAR1+#CHAR2+#CHAR3+#CHAR4+(CASE LEN(#n) WHEN 1 THEN '00' WHEN 2 THEN '0' ELSE '' END)+convert(VARCHAR(3),#n)
RETURN #nextSeriesNo
END

SQL Server insert loop of strings

Thank you in advance for your help here.
I want to insert incremental numbers as strings to load some bulk test numbers into a db, here is what i'm using:
Declare
#Serialcounter bigint
set #Serialcounter = 0
while #Serialcounter < 10
insert into [TrackTrace].[dbo].[TAB_ELEMENT] ([Serial], [Batch], [Batch_Id], [QCSample], [StationID])
values(Convert(varchar(60), #Serialcounter), 'test', 8989, 0, 1)
set #Serialcounter = (#Serialcounter + 1)
but when I do this it does not increment the counter and I just insert duplicate numbers and do not stop. I think my problem is that my variable is incremented outside of the while loop, but I am not sure how to rectify this.
Declare
#Serialcounter bigint
set #Serialcounter = 0
while #Serialcounter < 10
BEGIN
PRINT #Serialcounter
--insert into [TrackTrace].[dbo].[TAB_ELEMENT]
--([Serial]
--,[Batch]
--,[Batch_Id]
--,[QCSample]
--,[StationID])
--Values(Convert(varchar(60),#Serialcounter),'test',8989,0,1)
set #Serialcounter = (#Serialcounter +1 )
END
You not giving begin and end so as for all loops only first statement is considered
I was missing BEGIN and END statements
DECLARE
#Serialcounter BIGINT
SET #Serialcounter = 0
WHILE #Serialcounter < 10
BEGIN -- here
INSERT INTO [TrackTrace].[dbo].[TAB_ELEMENT]
([Serial]
,[Batch]
,[Batch_Id]
,[QCSample]
,[StationID])
VALUES(Convert(varchar(60),#Serialcounter),'test',8989,0,1)
SET #Serialcounter = (#Serialcounter +1 )
END -- and here

T-SQL Output Message During execution in SSMS

I have a simple query which loops and I want to see the PRINT messages during the execution. The query is something like this:
WHILE 1 = 1
BEGIN
WAITFOR DELAY '000:00:10'
PRINT 'here'
END
The PRINT 'here' does not output until I stop the process. However, I want to see it while it's running. Is this possible?
You can use RAISERROR with serverity 0 and the NOWAIT option
WHILE 1 = 1
BEGIN
WAITFOR DELAY '000:00:10'
RAISERROR ('here', 0, 1) WITH NOWAIT
END
I believe that the prints get buffered, releasing "chunks" as the buffer fills up.
try using raiserror:
How do I flush the PRINT buffer in TSQL?
Try this..
DECLARE #i INT = 1
WHILE ( #i <= 10)
BEGIN
--- do something
SELECT 'Completed ' + CAST(#i AS VARCHAR(50)) + ' : ' + CAST(GETDATE() AS VARCHAR(50));
SET #i = #i + 1
END
current suggestions don't work for SSMS 18 and above.
Print then raiserror(N'', 0, 1) seems to get around this.