Solved

Help with SQL syntax please..

Posted on 2016-08-16
10
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 52

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 51

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 38

Expert Comment

by:Geert Gruwez
ID: 41757756
move that code to a new procedure and then just call that new procedure
0
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 

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 38

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
 

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 51

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 14

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:Scott Pletcher
Scott Pletcher 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

623 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