incorrect syntax near in creating stored procedure on a remote db - sql

I am trying to recreate a stored procedure on a database which is hosted on a remote server.
When creating the same SP locally I don't get any errors but when I am trying to create the SP on the remote server I am getting this errors:
Msg 102, Level 15, State 1, Procedure MySP, Line 26
Incorrect syntax near 'MERGE'.
Msg 102, Level 15, State 1, Procedure MySP, Line 27
Incorrect syntax near 'S'.
Those errors are repeating in multiple SP and I didn't get them when I created the SP's on my local server.
Any idea what can that be?
Here is a sample SP:
USE [MyDatabase]
GO
/****** Object: StoredProcedure [dbo].[InsertContractorInfo] Script Date: 11/07/2012 01:08:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
--exec [dbo].[InsertContractorInfo]
CREATE PROCEDURE [dbo].[InsertContractorInfo]
(#CompanyName nvarchar(50),
#LicenseNumber nvarchar(50),
#MailingAddress1 nvarchar(200),
#MailingAddress2 nvarchar(200),
#Phone nvarchar(10),
#Fax nvarchar(10),
#Mobile nvarchar(10),
#Email nvarchar(200),
#PercentCommercial int,
#PercentResidental int,
#TotalEmployees int,
#AnnualSales decimal(18,2),
#InsuranceCompanyContact nvarchar(100)=null,
#InsuranceCompanyContactEmail nvarchar(100)=null,
#InsuranceCompanyContactPhone nvarchar(100)=null,
#ContractorID uniqueidentifier)
AS
BEGIN
MERGE dbo.Contractor con
USING(SELECT 1 S) S
ON con.Oid = #ContractorID
WHEN MATCHED THEN UPDATE
SET
[CompanyName] = #CompanyName
,[LicenseNumber] = #LicenseNumber
,[MailingAddress1] = #MailingAddress1
,[MailingAddress2] = #MailingAddress2
,[Phone] = #Phone
,[Fax] = #Fax
,[EMail] =#Email
,[Mobile] =#Mobile
,[PercentCommercial] = #PercentCommercial
,[PercentResidental] = #PercentResidental
,[TotalEmployees] = #TotalEmployees
,[ApproximateAnnualSales] = #AnnualSales
,[InsuranceCompanyContact] = #InsuranceCompanyContact
,[InsuranceCompanyContactPhone] = #InsuranceCompanyContactPhone
,[InsuranceCompanyContactEmail] = #InsuranceCompanyContactEmail
WHEN NOT MATCHED THEN
INSERT
([Oid]
,[CompanyName]
,[LicenseNumber]
,[MailingAddress1]
,[MailingAddress2]
,[Phone]
,[Fax]
,[EMail]
,[Mobile]
,[PercentCommercial]
,[PercentResidental]
,[TotalEmployees]
,[ApproximateAnnualSales]
,[InsuranceCompanyContact]
,[InsuranceCompanyContactPhone]
,[InsuranceCompanyContactEmail])
VALUES
(#ContractorId
,#CompanyName
,#LicenseNumber
,#MailingAddress1
,#MailingAddress2
,#Phone
,#Fax
,#EMail
,#Mobile
,#PercentCommercial
,#PercentResidental
,#TotalEmployees
,#AnnualSales
,#InsuranceCompanyContact
,#InsuranceCompanyContactPhone
,#InsuranceCompanyContactEmail
);
END
GO
Thanks, Laziale

This merger feature works with sql sever 2008 + versions , if you are trying to use in sql server 2005 you can do like this merge mimic in 2005
http://sqlserver-tips.blogspot.com/2006/09/mimicking-merge-statement-in-sql.html

You can try this also before CREATE PROCEDURE script.
USE [MyDatabase]
GO
IF EXISTS(SELECT Name FROM SysObjects WHERE ID = OBJECT_ID(N'[InsertContractorInfo]'))
BEGIN
DROP PROCEDURE InsertContractorInfo
END
GO

Related

Stored Procedures & SQL Server

I'm doing some revision in SQL Server and I have a question that I'm trying to work out, I'm meant to be creating a stored procedure that displays all details of branch name, book code, and quantity on hand,the stored procedure takes parameter called #BranchName. I'm also meant to use EXEC to call this procedure with a value for the parameter.
This is my statement so far (I know that it's wrong and doesn't work some reason, I can't work it out)
CREATE PROCEDURE BranchDetails
SELECT
B.BookCode, BR.BranchName, I.OnHand
FROM
BOOK, BRANCH, INVENTORY
WHERE
BranchName = 'BookCode'
MSSQL Syntax
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName>
-- Add the parameters for the stored procedure here
<#Param1, sysname, #p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
<#Param2, sysname, #p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT <#Param1, sysname, #p1>, <#Param2, sysname, #p2>
END
GO
to solve your issue.
CREATE PROCEDURE BranchDetails #BranchCode varchar(20)
as
BEGIN
SELECT B.BookCode, BR.BranchName, I.OnHand
FROM BOOK, BRANCH, INVENTORY
WHERE BranchCode = #BranchCode
END
To execute
exec BranchDetails 'Disneyland'

Table-valued parameter error in SQL Server

I'm working on a reporting module for a company project. Although we use an ORM for our application, I've decided to write stored procedures for the reports in anticipation of migrating to SSRS.
These stored procedures require table-valued parameter input. As such, I've created my table type:
USE MyDatabase
GO
/****** Object: UserDefinedTableType [dbo].[IntList] Script Date: 5/8/2013 5:20:59 PM ******/
CREATE TYPE [dbo].[IntList] AS TABLE(
[Id] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
GO
I have the following SQL Server stored proc:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
USE MyDatabase
GO
-- =============================================
-- Author: <lunchmeat317>
-- Create date: <05/06/2013>
-- Description: <File Type Report>
-- =============================================
ALTER PROCEDURE Report_FileType
#filetype varchar(20) = null,
#User intList READONLY,
#Group intList READONLY
AS
BEGIN
SET NOCOUNT ON;
/*
lf = LibraryFile
lfu = LibraryFileAssignedUser
lfg = LibraryFileAssignedGroup
*/
SELECT Extension AS FileType, COUNT(1) AS TotalFiles
FROM LibraryFile lf
LEFT JOIN LibraryFileAssignedUser lfu
ON (SELECT COUNT(1) FROM #User) != 0
AND lfu.LibraryFileId = lf.Id
LEFT JOIN LibraryFileAssignedGroup lfg
ON (SELECT COUNT(1) FROM #Group) != 0
AND lfg.LibraryFileId = lf.Id
WHERE ((#filetype IS NULL) OR (Extension = #filetype))
AND (
((#User IS NULL) OR (lfu.UserId IN (SELECT * FROM #User)))
OR ((#Group IS NULL) OR (lfg.HubGroupId IN (SELECT * FROM #Group)))
)
GROUP BY Extension
END
GO
When I attempt to alter the stored procedure, I continually get the error message
Msg 137, Level 16, State 1, Procedure Report_FileType
Must declare the scalar variable "#User".
Msg 137, Level 16, State 1, Procedure Report_FileType
Must declare the scalar variable "#Group".
I can't figure out why this is happening. If I do use a scalar type (and update my code to match) it works. However, when I try to use a TVP, I can't compile the stored procedure.
For what it's worth, I've added the type, but I haven't set the permission on it yet. However, I don't expect that would cause a compilation error; it would only cause an error at runtime (which I've dealt with before).
Does anyone have any experience with this issue? Thanks!
Interesting, I haven't used this before, but it seems that you cannot test for #User / #Group IS NULL since it is a table.
Cf.
https://dba.stackexchange.com/questions/30754/how-do-i-check-for-a-null-or-empty-table-valued-parameter
http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/c59f6b82-7639-42c7-ad90-a4ec7315a3bd/

Error in SQL Server 2005 stored procedure

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Prc_InsertUpdate] (#boxone VARCHAR(200),
#boxtwo VARCHAR(200),
#boxthree VARCHAR(200))
AS
DECLARE #num AS INT
SELECT #num = MAX(NUMBER) + 1
FROM updatepage
INSERT INTO [TestDB].[dbo].[updatepage]
([number],
[box1],
[box2],
[box3])
VALUES (#num,
#boxone,
#boxtwo,
#boxthree)
I'm creating this procedure but got this error
Msg 208, Level 16, State 6, Procedure Prc_InsertUpdate, Line 9
Invalid object name 'dbo.Prc_InsertUpdate'.
You are ALTER-ing a stored procedure that does not exist. Use CREATE procedure [dbo].[Prc_InsertUpdate] instead.
Also why isn't number an identity column? Your current approach is inefficient and not safe under conditions of concurrency?

create trigger using a stored procedure

I have a trigger and I want to kick it off from a stored procedure. I am using ms access and when i run the trigger from ms access it gives me an error msg (ODBC). I think I can't create triggers using ms access. This is my trigger:
IF EXISTS
(SELECT name
FROM sys.objects
WHERE name = 'UpdateComments' AND type = 'TR')
DROP TRIGGER tblEmailHdr_abenit01.UpdateComments;
GO
CREATE TRIGGER UpdateComments
ON tblEmailHdr_abenit01
AFTER Update
AS
IF ( UPDATE (Comments) ) BEGIN Update ttblEmailHdr_abenit01
Set UpdateComm = GetDate()
END;
GO
This is how I have been trying to create the trigger from the stored procedure but I get the following error msg's when I try to create the sproc:
Sproc:
CREATE PROCEDURE dbo.SP_AS_tblEmailHdr_Trig (#UserID as varchar(10))
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
--SET NOCOUNT ON;
-- Insert statements for procedure here
Declare #UserTable Varchar(50)
Declare #UserTable2 Varchar(50)
Set #UserTable = 'tblEmailHdr_' + #UserID ;
Set #UserTable2 = 'tblEmailHdr_' + #UserID + '.UpdateComments' ;
IF EXISTS
(SELECT name
FROM sys.objects
WHERE name = 'UpdateComments' AND type = 'TR') DROP TRIGGER #UserTable2
GO
CREATE TRIGGER UpdateComments
ON #UserTable
AFTER UPDATE
AS
IF ( UPDATE (Comments) )
BEGIN
--RAISERROR (50009, 16, 10)
Update #UserTable
Set UpdatedComm = GetDate()
END
GO
END
GO
error msg i get:
Msg 102, Level 15, State 1, Procedure SP_AS_tblEmailHdr_Trig, Line 23
Incorrect syntax near '#UserTable2'.
Msg 102, Level 15, State 1, Procedure UpdateComments, Line 2
Incorrect syntax near '#UserTable'.
Msg 1087, Level 15, State 2, Procedure UpdateComments, Line 8
Must declare the table variable "#UserTable".
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'END'.
create procedure pro (parameters)
as
begin
declare #trigs nvarchar(max)
declare #trip nvarchar(max)
set #trigs='
create trigger tri on dbo.employee
for insert
as
select * from inserted
go'
set #trip='drop trigger tri'
EXEC sp_executeSQL #trigs
insert into employee values(parameters)
EXEC sp_executeSQL #trip
end
exec pro param
eg:
exec pro 80,'aaa','AAS',25000,'2013-02-01','iT'
remove all the GO statements from inside the procedure.

Stored procedure parses correctly but will not execute. Invalid object name. Msg 208

I've scripted up a stored procedure as follows. It will parse without errors, but when I try to execute it, it will fail. The error message reads: Msg 208, Level 16, State 6, Procedure aspnet_updateUser, Line 23
Invalid object name 'dbo.aspnet_updateUser'.
Here is the stored procedure.
USE [PMRS2]
GO
/****** Object: StoredProcedure [dbo].[aspnet_updateUser] Script Date: 05/25/2009 15:29:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[aspnet_updateUser]
-- Add the parameters for the stored procedure here
#UserName nvarchar(50),
#Email nvarchar(50),
#FName nvarchar(50),
#LName nvarchar(50),
#ActiveFlag bit,
#GroupId int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
UPDATE dbo.aspnet_Users
SET UserName = #UserName, LoweredUserName = LOWER(#UserName), Email = #Email, FName = #FName, LName = #LName, ActiveFlag = #ActiveFlag, GroupId = #GroupId
WHERE LoweredUserName = LOWER(#UserName)
END
Looks like it might not exist yet, swap the Alter to a Create.
To avoid this happening in the furture, do what we do, never use alter proc. Instead we check for the existance of the proc and drop it if it exists, then create it with the new code:
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'myProc')
BEGIN
DROP Procedure myProc
END
GO
CREATE PROCEDURE myProc
(add the rest of the proc here)
Here is another solution
USE [PMRS2]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID ( 'dbo.YourProcedureName', 'P' ) IS NOT NULL
DROP PROCEDURE dbo.YourProcedureName;
GO
CREATE PROCEDURE [dbo].[YourProcedureName] (
#UserName varchar(50),
#Password varchar(50))
AS
BEGIN
SET NOCOUNT ON;
select ... (your query)
END