Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

Converting Stored Proc to Inline Query

$
0
0
Hello,

I have a stored proc that is being executed from an OLE DB Source component in my Data Flow. Takes one input parm, has several variables declared within, a derived table which is used in a join and all within a try catch block with transaction handling. No updates, just returning data, works great, except now I have been asked to replace these stored procs with inline queries.
ALTER PROCEDURE [dbo].[usp_Get_Test]
(
	@numberOfMonths				int
)
AS
BEGIN
	SET NOCOUNT ON
	set transaction isolation level read uncommitted

declare @TESTY table (TEST_ID char(12), TEST_SEQ_NO smallint, TEST_ID_ADJ_FROM char(12), TEST_IND char(1))

declare @BeginDate datetime
        ,@EndDate datetime
        ,@TestKey bigint
        ,@TestNumber varchar(255)

select   @BeginDate = dateadd(MONTH, @numberOfMonths, GETDATE())
select   @EndDate = GETDATE()

BEGIN TRANSACTION

BEGIN TRY

insert into @TESTY
        ( TEST_ID ,
          TEST_SEQ_NO ,
          TEST_ID_ADJ_FROM ,
          TEST_IND
        )
select  tl.TEST_ID
,       cl.TEST_SEQ_NO
,       tl.TEST_ID_ADJ_FROM
,       case
            when cl.TEST_ID is null then 'N'
            else 'Y'
        end as TEST_IND
from    TESTL tl
join    TESTCL cl on tl.TEST_ID = cl.TEST_ID
where   tl.TEST_DT > @BeginDate
        and tl.TEST_DT < @EndDate

select	td.TestNumber as [TEST_ID]

from    TestDetail td

left
join    @TESTY TY on td.TESTDataSource = 'TESTY' and td.TestNumber = TY.TEST_ID
        and TY.TEST_SEQ_NO = td.TestLineNumber

where   td.TestMonth > @BeginDate
        and td.TestMonth < @EndDate
        and td.TESTStatus in('tested','retested')

END TRY
BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage

    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION
END CATCH

IF @@TRANCOUNT > 0
    COMMIT TRANSACTION
END
The problems I have run into so far are...

SQL command text in OLE DB Source Editor does not like:
- TRY/CATCH block
- Will not let me use my input parm (@numberOfMonths int)
- When I hard code in my input parm (select   @BeginDate = dateadd(MONTH, -1, GETDATE())) I can parse query and run the step but no results are returned. So I am let to assume that it does not like the @TESTY derived table.

The query here as a sample has had pivots removed as well, but research suggests this should be an issue in the SQL command text.

Also, I know not even to try the Build Query... cause it will complain about any variable 
declarations (i.e., declare @BeginDate datetime).

So, if someone could confirm some things for me I would really appreciate it. For instance, can I pull this off with an Execute SQL Task? The problem is I don't see this available in the toolbox for the Data Flow. 

Also, would my error handling be done in the Event Handlers tab now and if so, is there a good example of this?

Thanks,
Buster

BusterCoder


Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>