HOW TO EXECUTE 2-3 SQL STATMENTS ONE AFTER ANOTHER BASED ON THE RESULT OF PREVIOUS QUERY?

  • Ok...I think I am very new to this forum..So if somthing has been done wrong, than it must be unintensionally.

    I will now create a one topic and stick with it.

    sorry for any incovieniance.

    thanks.

  • prtk.raval (6/3/2013)


    Ok...I think I am very new to this forum..So if somthing has been done wrong, than it must be unintensionally.

    I will now create a one topic and stick with it.

    sorry for any incovieniance.

    thanks.

    It is not that you did anything wrong. You are however doing yourself an injustice by creating all these threads and not providing all the details. The people around who post on these forums do so because they enjoy helping others. We don't get paid for this. When we try to help people and the target keeps moving it is very frustrating. Among all of the threads you have started on this topic I am convinced you should pick one of them and post ALL of the details about what you are trying to do. Post all the table structures, create some sample data, explain your ETL process in detail. When we get disconnected details like in this thread we don't come up with a solution that will work because we don't know the whole story.

    I hope you will understand why it is so important to post all the details and I truly hope you are able to figure out how to solve your issue.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • here's a quick and dirty example, of how to get rows from the Oracle Linked server without copying the whole table, and then filtering, like a typical linked server query would do.

    see how in this case, i'm using the EXECUTE (...)AT syntax, which performs teh work on the linked server and returns the results.

    since that format cannot accept variables or concatenated strings, I have to use dynamic SQL to get the results, and i'm inserting those results into a temp table:

    --Get the highest value modified to date.

    DECLARE @HighestValue datetime;

    SELECT @HighestValue = ModifiedDate From MySQLServer.dbo.MyTable

    Create Table #Results(id int identity(1,1) not null primary key, ColumnList int,SomeOtherData varchar(30) );

    DECLARE @cmd varchar(2000);

    SET @cmd = 'EXECUTE ( ''SELECT ColumnList,SomeOtherData From MyTableInOracle WHERE LastModifiedDate > TO_DATE(''' + CONVERT(VARCHAR,@HighestValue,112) + ''',''YYYYMMDD'') '' ) AT MyOracleLinkedServer ';

    INSERT INTO #Results(ColumnList,SomeOtherData)

    EXEC(@cmd);

    EXEC(@cmd);

    there's a LOT of details missing here: what are the PK's of the data, the actual columns, the linked server name, etc.

    lots of details to flesh out this example with.

    once you have that much shorter list of changes, you should be able to merge/insert/update the data on the SQL server side a lot easier.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 3 posts - 16 through 17 (of 17 total)

You must be logged in to reply to this topic. Login to reply