Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS SQL syntax question

Posted on 2007-04-10
2
Medium Priority
?
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

636 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