?
Solved

MS SQL syntax question

Posted on 2007-04-10
2
Medium Priority
?
217 Views
Last Modified: 2010-03-19
What is the correct syntax for this SP?   I want to create a SP to recreate some triggers in a DB.   Here is what I have tried.

CREATE PROCEDURE dbo.CreateTriggersInPL2DB
      
AS

      
create TRIGGER PL2Enhancement_addEbayAuction
ON POWERLINK.dbo.INVENTORY
FOR insert
AS
insert into PLEnhancements.dbo.ebayListingQueue

(
InventoryID,
CreatedOn,
ActionToPerform,
Status
)

select
inventoryID,
getdate(),
'addItem',
null

from inserted

Create TRIGGER PL2Enhancement_addProductToSF
ON dbo.INVENTORY
FOR insert
AS
declare @inventoryID int
select @InventoryID=inventoryid from inserted

exec PLEnhancements.dbo.InsertProductIntoSFbyInventoryID @InventoryID
      
RETURN
0
Comment
Question by:pamsauto
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 18882147
CREATE scripts have to be isolated, with the exception of temp tables.
so the only solution is to make it dynamic sql:


CREATE PROCEDURE dbo.CreateTriggersInPL2DB
     
AS

exec ('      
create TRIGGER PL2Enhancement_addEbayAuction
ON POWERLINK.dbo.INVENTORY
FOR insert
AS
insert into PLEnhancements.dbo.ebayListingQueue

(
InventoryID,
CreatedOn,
ActionToPerform,
Status
)

select
inventoryID,
getdate(),
''addItem'',
null

from inserted
')

exec ('
Create TRIGGER PL2Enhancement_addProductToSF
ON dbo.INVENTORY
FOR insert
AS
declare @inventoryID int
select @InventoryID=inventoryid from inserted

exec PLEnhancements.dbo.InsertProductIntoSFbyInventoryID @InventoryID
')
     
RETURN

0
 
LVL 27

Expert Comment

by:ptjcb
ID: 18882156
Why are you trying to do this within a stored procedure? Why not just script the triggers? Are you planning to run this stored procedure often?

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question