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
Solved

MS SQL syntax question

Posted on 2007-04-10
2
203 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 500 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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…

856 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