Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stored procedure to run results of view


Stored procedure to run results of view

Author
Message
young one
young one
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
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'
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36170 Visits: 18751
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
My Blog: www.voiceofthedba.com
young one
young one
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
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 Smile
young one
young one
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
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
HanShi
HanShi
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3049 Visits: 3633
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’! **
young one
young one
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 92
Thanks Han Shi,

I will have a look at this method!
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8997 Visits: 19028
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8997 Visits: 19028
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
young one
young one
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8997 Visits: 19028
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search