Solved

Help with SQL syntax please..

Posted on 2016-08-16
10
106 Views
Last Modified: 2016-08-26
Hi,

I have an SQL Agent job that needs to do the following..

sql agent
Please help!
0
Comment
Question by:SmashAndGrab
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41757753
try write a stored procedure that within itself it will doing the relevant looping to call another stored procedure. so that in your SQL Agent job, it only requires to call one single stored procedure.

hope this give you some idea to do it.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41757754
You need to declare variable @code and store the Code on it. I'm guessing is INT but if not then change to the correct data type:
DECLARE @Code INT

SELECT @Code = Code 
FROM FF_TALLY_TABLE
WHERE Optio_Concat IS NULL

EXEC sp_name @Code

Open in new window

0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 41757756
move that code to a new procedure and then just call that new procedure
0
 

Author Comment

by:SmashAndGrab
ID: 41757811
Thanks guys.

So, I've taken your advice and created another stored procedure.

Referenced it here..

SQLJOB2.JPG

Now the bit I am not sure about..

 
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

Alter PROCEDURE [dbo].[FF_spOptio_Concat_Checker]

AS

BEGIN

DECLARE @Code			int


Select Code FROM  FF_TALLY_TABLE WHERE(Optio_Concat IS NULL)


//Now execute the original stored procedure
EXEC Optio_Concat @Code

END

Open in new window



The query could bring back more than 1 record, so I need to execute the stored procedure (EXEC Optio_Concat @Code), potentially, multiple times.

Hope I've made sense.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 41757820
use google: transact sql loop through records
you'll probably follow this item:
https://support.microsoft.com/en-us/kb/111401
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:SmashAndGrab
ID: 41757826
I found this..

Thought it had worked and it didn't :(



set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

Alter PROCEDURE [dbo].[FF_spOptio_Concat_Checker]

AS

BEGIN

DECLARE @Code			int

DECLARE db_cursor CURSOR FOR Select Code FROM FF_TALLY_TABLE WHERE(Optio_Concat IS NULL)

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @Code

WHILE @@FETCH_STATUS = 0   
BEGIN   
       EXEC Optio_Concat @Code
END   

CLOSE db_cursor   
DEALLOCATE db_cursor 

Open in new window

0
 

Author Comment

by:SmashAndGrab
ID: 41757829
db cursor
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 250 total points
ID: 41757879
You have an infinite loop. Inside the WHILE you'll need to fetch the next record or otherwise it will keep processing the same record forever.
Also, you have a BEGIN but not an END block. Anyway, you won't need it for a Stored Procedure so I've removed it:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

Alter PROCEDURE [dbo].[FF_spOptio_Concat_Checker]

AS

DECLARE @Code int

DECLARE db_cursor CURSOR FOR 
Select Code 
FROM FF_TALLY_TABLE 
WHERE Optio_Concat IS NULL

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @Code

WHILE @@FETCH_STATUS = 0   
BEGIN   
       EXEC Optio_Concat @Code
       FETCH NEXT FROM db_cursor INTO @Code
END   

CLOSE db_cursor   
DEALLOCATE db_cursor 

GO

Open in new window

0
 
LVL 11

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 125 total points
ID: 41757883
That's because you don't have the "END" for the stored procedure itself.

ALTER PROCEDURE...
AS
BEGIN
....
WHERE @@FETCH_STATUS = 0
   BEGIN
   END
...
CLOSE db_cursor
DEALLOCATE db_cursor

END -- This "END" appears to be missing
1
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 125 total points
ID: 41757958
Or concat an execution string instead of looping:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[FF_spOptio_Concat_Checker]

AS

BEGIN

DECLARE @Code                  int
DECLARE @sql            varchar(max)

SELECT @sql = (
    SELECT 'EXEC dbo.Optio_Concat ' + CAST(Code AS varchar(10)) + ';'
    FROM dbo.FF_TALLY_TABLE
    WHERE Optio_Concat IS NULL
    ORDER BY Code
    FOR XML PATH('')
    )

--PRINT @sql
EXEC(@sql)

END
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

My client sends a request to me that they want me to load data, which will be returned by Web Service APIs, and do some transformation before importing to database. In this article, I will provide an approach to load data with Web Service Task and X…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now