Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Stored procedure to run results of view Expand / Collapse
Author
Message
Posted Wednesday, September 4, 2013 6:54 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, December 16, 2013 11:53 PM
Points: 31, Visits: 92
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'
Post #1491264
Posted Wednesday, September 4, 2013 7:55 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 4:15 PM
Points: 31,210, Visits: 15,654
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








Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1491319
Posted Wednesday, September 4, 2013 8:09 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, December 16, 2013 11:53 PM
Points: 31, Visits: 92
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 :)
Post #1491325
Posted Thursday, September 5, 2013 1:11 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, December 16, 2013 11:53 PM
Points: 31, Visits: 92
I just want a separate job that will automatically execute the results of the select statement instead of running it manually one by one
Post #1491639
Posted Thursday, September 5, 2013 1:28 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 27, 2014 8:18 AM
Points: 2,404, Visits: 2,940
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’! **
Post #1491648
Posted Thursday, September 5, 2013 1:34 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, December 16, 2013 11:53 PM
Points: 31, Visits: 92
Thanks Han Shi,

I will have a look at this method!
Post #1491650
Posted Thursday, September 5, 2013 1:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:52 AM
Points: 6,826, Visits: 14,062
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1491660
Posted Thursday, September 5, 2013 2:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:52 AM
Points: 6,826, Visits: 14,062
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1491662
Posted Thursday, September 5, 2013 2:08 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, December 16, 2013 11:53 PM
Points: 31, Visits: 92
This seems like the thing I'm looking for.

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

I am busy investigating

Thanks
Post #1491665
Posted Thursday, September 5, 2013 2:28 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:52 AM
Points: 6,826, Visits: 14,062
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1491670
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse