Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with SQL syntax please..

Posted on 2016-08-16
10
Medium Priority
?
149 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 53

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 52

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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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 52

Accepted Solution

by:
Vitor Montalvão earned 1000 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 500 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

721 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