Stored procedure to run results of view

  • Hi everyone,

    Was hoping someone can help me with the following. We have a view that selects a selection of data and returns the results to "insert" statements that need to be manually run. I was wondering if there is a way to create a stored procedure to automatically run/execute the results from this view. I am fairly new to sql and would appreciate the assistance.

    View:

    SELECT TOP (100)

    PERCENT 'Insert into Mobitech_Filtered_Task_Step (task_step_id,task_id,task_step_type_Id, display_order,int_data, varchar_data,image_data,bit_data,decimal_data,float_data,lat_data,lon_data,notes,completed,deleted,date_added, server_date_modified,added_by,modified_by,meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified) select task_step_id,task_id,task_step_type_Id,display_order,int_data,varchar_data,image_data,bit_data,decimal_data, float_data,lat_data,lon_data,notes,completed,deleted,date_added,server_date_modified,added_by,modified_by, meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified

    from [23.23.209.111].[mobitechGeneric_qa].[dbo].[filtered_task_step] where task_id ='''

    + CAST(dbo.Mobitech_Filtered_Task.task_id AS varchar(100)) + '''' AS Expr1

    FROM dbo.Mobitech_Filtered_Task LEFT OUTER JOIN

    dbo.Mobitech_Filtered_Task_Step ON dbo.Mobitech_Filtered_Task.task_id = dbo.Mobitech_Filtered_Task_Step.task_id

    WHERE (dbo.Mobitech_Filtered_Task.PIMS_IMPORTED IS NULL) OR

    (dbo.Mobitech_Filtered_Task.PIMS_IMPORTED = 0)

    GROUP BY dbo.Mobitech_Filtered_Task.task_id, dbo.Mobitech_Filtered_Task.completed, dbo.Mobitech_Filtered_Task.mobile_date_modified,

    dbo.Mobitech_Filtered_Task.date_added,

    'Insert into Mobitech_Filtered_Task_Step (task_step_id,task_id,task_step_type_Id,display_order,int_data,varchar_data,image_data,bit_data,decimal_data, float_data,lat_data,lon_data,notes,completed,deleted,date_added,server_date_modified,added_by,modified_by,meter_formula, number_of_dials,additional_info,web_date_modified,mobile_date_modified) select task_step_id,task_id,task_step_type_Id,display_order,int_data,varchar_data,image_data,bit_data,decimal_data,float_data,lat_data, lon_data,notes,completed,deleted,date_added,server_date_modified,added_by,modified_by,meter_formula,number_of_dials, additional_info,web_date_modified,mobile_date_modified

    from [23.23.209.111].[mobitechGeneric_qa].[dbo].[filtered_task_step] where task_id ='''

    + CAST(dbo.Mobitech_Filtered_Task.task_id AS varchar(100)) + ''''

    HAVING (dbo.Mobitech_Filtered_Task.completed = 1) AND (NOT (dbo.Mobitech_Filtered_Task.mobile_date_modified IS NULL)) AND

    (COUNT(dbo.Mobitech_Filtered_Task_Step.task_id) = 0) AND (dbo.Mobitech_Filtered_Task.date_added > CONVERT(DATETIME, '2013-08-03 00:00:00', 102))

    ORDER BY dbo.Mobitech_Filtered_Task.mobile_date_modified

    Example of returned rows:

    Insert into Mobitech_Filtered_Task_Step (task_step_id,task_id,task_step_type_Id,display_order,int_data,varchar_data,image_data,bit_data,decimal_data,float_data,lat_data,lon_data,notes,completed,deleted,date_added,server_date_modified,added_by,modified_by,meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified) select task_step_id,task_id,task_step_type_Id,display_order,int_data,varchar_data,image_data,bit_data,decimal_data,float_data,lat_data,lon_data,notes,completed,deleted,date_added,server_date_modified,added_by,modified_by,meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified from [23.23.209.111].[mobitechGeneric_qa].[dbo].[filtered_task_step] where task_id ='E02F868B-6447-404F-8626-01B2C4951B82'

    Insert into Mobitech_Filtered_Task_Step (task_step_id,task_id,task_step_type_Id,display_order,int_data,varchar_data,image_data,bit_data,decimal_data,float_data,lat_data,lon_data,notes,completed,deleted,date_added,server_date_modified,added_by,modified_by,meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified) select task_step_id,task_id,task_step_type_Id,display_order,int_data,varchar_data,image_data,bit_data,decimal_data,float_data,lat_data,lon_data,notes,completed,deleted,date_added,server_date_modified,added_by,modified_by,meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified from [23.23.209.111].[mobitechGeneric_qa].[dbo].[filtered_task_step] where task_id ='47391029-564A-45E9-A808-12D4194C1161'

  • You don't have a view. You have a query and some statements.

    a view is just like a table. You query it with a select.

    A stored procedure is a set of statements, like you've listed at the bottom, that you enclose in the stored procedure structure. I'm not sure what you want to run or why you find this to be hard. If I want to run a

    insert into tableB select x from tableB

    in a stored procedure, I just put it in.

    create procedure myproc

    as

    begin

    insert into tableB select x from tableB

    end

  • Hi Steve,

    Thanks for the reply, after checking my initial post I realized I explained a bit wrong.

    This view I am talking about has 2 tables (diagram pane). From these 2 tables I have a few columns selected that belong to these tables (criteria pane). In the criteria pane there also an insert statement under the column column. Then in the sql pane I have that select statement I posted earlier. When this is executed, it returns the results into insert statements which then needs to be run separately. I want to know if I can somehow edit that script to automatically execute the insert results.

    Also, I have created a test database so I can play around a bit. Just so everyone knows I'm not asking them to do my job for me. I am here to learn 🙂

  • I just want a separate job that will automatically execute the results of the select statement instead of running it manually one by one

  • Please take a step back and take a good look at your requirements.

    Why do you want to insert the data using seperate insert statements? The performance will be a lot better if you take a set-based approach. So alter the query to not return insert statements, but rather return a complete set of data that needs to be inserted. Then you can use this result set to be inserted in the destination table at once.

    INSERT INTO {your_table}

    SELECT {column list}

    FROM {rest of your query}

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • Thanks Han Shi,

    I will have a look at this method!

  • I reckon, what you are looking for is this:

    INSERT INTO Mobitech_Filtered_Task_Step (

    task_step_id,task_id,task_step_type_Id, display_order,int_data, varchar_data,image_data,bit_data,decimal_data,

    float_data,lat_data,lon_data,notes,completed,deleted,date_added, server_date_modified,added_by,modified_by,

    meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified)

    SELECT

    task_step_id,task_id,task_step_type_Id, display_order,int_data, varchar_data,image_data,bit_data,decimal_data,

    float_data,lat_data,lon_data,notes,completed,deleted,date_added,server_date_modified,added_by,modified_by,

    meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified

    FROM [23.23.209.111].[mobitechGeneric_qa].[dbo].[filtered_task_step]

    WHERE task_id IN (

    SELECT t.task_id

    FROM dbo.Mobitech_Filtered_Task t

    LEFT OUTER JOIN dbo.Mobitech_Filtered_Task_Step ts

    ON t.task_id = ts.task_id

    WHERE t.PIMS_IMPORTED IS NULL OR t.PIMS_IMPORTED = 0

    GROUP BY

    t.task_id,

    t.completed,

    t.mobile_date_modified,

    t.date_added

    HAVING t.completed = 1

    AND t.mobile_date_modified IS NOT NULL

    AND COUNT(ts.task_id) = 0

    AND t.date_added > CONVERT(DATETIME, '2013-08-03 00:00:00', 102)

    )

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Which I think is the same as this:

    INSERT INTO Mobitech_Filtered_Task_Step (

    task_step_id,task_id,task_step_type_Id, display_order,int_data, varchar_data,image_data,bit_data,decimal_data,

    float_data,lat_data,lon_data,notes,completed,deleted,date_added, server_date_modified,added_by,modified_by,

    meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified)

    SELECT

    task_step_id,task_id,task_step_type_Id, display_order,int_data, varchar_data,image_data,bit_data,decimal_data,

    float_data,lat_data,lon_data,notes,completed,deleted,date_added, server_date_modified,added_by,modified_by,

    meter_formula,number_of_dials,additional_info,web_date_modified,mobile_date_modified

    FROM [23.23.209.111].[mobitechGeneric_qa].[dbo].[filtered_task_step] f

    WHERE f.task_id IN (

    SELECT t.task_id

    FROM dbo.Mobitech_Filtered_Task t

    WHERE (t.PIMS_IMPORTED IS NULL OR t.PIMS_IMPORTED = 0)

    AND t.completed = 1

    AND t.mobile_date_modified IS NOT NULL

    AND t.date_added > CONVERT(DATETIME, '2013-08-03 00:00:00', 102)

    )

    AND NOT EXISTS (

    SELECT 1

    FROM dbo.Mobitech_Filtered_Task_Step ts

    WHERE ts.task_id = f.task_id

    )

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • This seems like the thing I'm looking for.

    Both are however returning "Invalid column name 'PIMS_IMPORTED",

    I am busy investigating

    Thanks

  • I've no idea, because it's in the original query:

    Original:

    WHERE

    (dbo.Mobitech_Filtered_Task.PIMS_IMPORTED IS NULL) OR

    (dbo.Mobitech_Filtered_Task.PIMS_IMPORTED = 0)

    New:

    SELECT t.task_id

    FROM dbo.Mobitech_Filtered_Task t

    WHERE (t.PIMS_IMPORTED IS NULL OR t.PIMS_IMPORTED = 0)

    AND t.completed = 1

    AND t.mobile_date_modified IS NOT NULL

    AND t.date_added > CONVERT(DATETIME, '2013-08-03 00:00:00', 102)

    Does the original query actually run without errors?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I got it, this was an old copy of the original database i tested on but it didnt have that column. I created a new db copy and am busy running that query

  • The copy database was a bit older and didn't contain that column. I created a new test db and restored this mornings backup.

    Ran the query, but it errors out with the following:

    OLE DB provider "SQLNCLI10" for linked server "23.23.209.111" returned message "Protocol error in TDS stream".

    OLE DB provider "SQLNCLI10" for linked server "23.23.209.111" returned message "Communication link failure".

    Msg 65535, Level 16, State 1, Line 0

    Session Provider: Physical connection is not usable [xFFFFFFFF].

    OLE DB provider "SQLNCLI10" for linked server "23.23.209.111" returned message "Communication link failure".

    Msg 65535, Level 16, State 1, Line 0

    Session Provider: Physical connection is not usable [xFFFFFFFF].

    OLE DB provider "SQLNCLI10" for linked server "23.23.209.111" returned message "Communication link failure".

    Msg 10054, Level 16, State 1, Line 0

    TCP Provider: An existing connection was forcibly closed by the remote host.

    Suspect it might be a network issue, as it can't be permissions (using sa account)

    Will investigate some more

  • Not sure my replies are going through. The column issue has been resolved.

    Getting the following errors:

    OLE DB provider "SQLNCLI10" for linked server "23.23.209.111" returned message "Protocol error in TDS stream".

    OLE DB provider "SQLNCLI10" for linked server "23.23.209.111" returned message "Communication link failure".

    Msg 65535, Level 16, State 1, Line 0

    Session Provider: Physical connection is not usable [xFFFFFFFF].

    OLE DB provider "SQLNCLI10" for linked server "23.23.209.111" returned message "Communication link failure".

    Msg 65535, Level 16, State 1, Line 0

    Session Provider: Physical connection is not usable [xFFFFFFFF].

    OLE DB provider "SQLNCLI10" for linked server "23.23.209.111" returned message "Communication link failure".

    Msg 10054, Level 16, State 1, Line 0

    TCP Provider: An existing connection was forcibly closed by the remote host.

    Might be a network connection either to or from the linked server. I can ping it fine though. Can't be permissions as I am using the sa account

    Busy investigating

  • You may get better performance if you detach the SELECT from the INSERT by running the results of the SELECT into a temp table.

    Also, since your SELECT reads local and remote data, the INNER REMOTE JOIN hint may work for you depending on the relative size of the two tables.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thanks Chris!!

    I will have a look at that

Viewing 15 posts - 1 through 14 (of 14 total)

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