Error in SQL Server 2005 stored procedure - sql-server-2005

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?

Related

Msg 137, Level 15, State 2, Server ip-172-31-36-11, Line 2 Must declare the scalar variable "#Deptno"

My stored procedure (below) gives the following error:
Msg 137, Level 15, State 2, Server ip-172-31-36-11, Line 2 Must declare the scalar variable "#Deptno"
CREATE PROCEDURE EmployeesDept
#Deptno char(3)
AS
SELECT lastname AS Name
FROM Employee
WHERE workdept = #Deptno
GO
EXECUTE EmployeesDept #Deptno
GO
When it comes to executing a stored procedure you have to give the parameters values if you want it to work, and you can execute positionally or by name. You can put values in directly, or you can store values in variables and use those variables to give values to the stores procedure
--direct values
execute EmployeesDept 'abc', 0 --positional
execute EmployeesDept #Deptno='abc', #whatever=0 --named parameters
--values from variables
DECLARE #n CHAR(3) = 'abc';
DECLARE #i INT = 0;
execute EmployeesDept #n, #i --position based
execute EmployeesDept #Deptno = #n, #whatever = #i --name based
Named based parameters do not have to be in order, positional ones do
A better habit (for reasons of taking your skills to another DB) for CREATE PROCEDURE is like:
CREATE PROCEDURE EmployeesDept (
#Deptno char(3)
) AS
with parentheses around the argument list. For 2 or more args, separate with a comma. Example:
CREATE PROCEDURE EmployeesDept (
#Deptno char(3),
#whatever INT
) AS
Also get into the habit of ending each statement in a stored procedure with a semicolon

Operand type clash when compiling a native stored procedure in SQL Server 2019

Any idea why I can compile this stored procedure with the first Insert but not the second (or both)? The error message is:
Msg 206, Level 16, State 2, Procedure InsertExtPageWithXML, Line 21 [Batch Start Line 11]
Operand type clash: numeric is incompatible with uniqueidentifier
THis is the SQL code:
--======================================================
-- Create Natively Compiled Stored Procedure Template
--======================================================
USE [PortalMO]
GO
-- Drop stored procedure if it already exists
IF OBJECT_ID('InsertExtPageWithXML','P') IS NOT NULL
DROP PROCEDURE [dbo].[InsertExtPageWithXML]
GO
CREATE PROCEDURE [dbo].[InsertExtPageWithXML]
-- Add the parameters for the stored procedure here
-- (not inserted is the auto-generated UI [PK_Id], non-null, primary key..)
(#1_Topic_PK uniqueidentifier = NULL,
#1_Path nvarchar(500) = "fix.me",
#1_Title nvarchar(450) = "fix.me",
#1_URL nvarchar(max) = "fix.me",
#1_Priority tinyint = NULL,
#1_Type nvarchar(50) = NULL,
#2_XMLfragment nvarchar(max) = "fix.me")
-- (all of the types above are accurate to the schemas already in existence)
WITH NATIVE_COMPILATION, SCHEMABINDING
AS BEGIN ATOMIC WITH
(
TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english'
)
--Insert statements for the stored procedure here
INSERT INTO [dbo].[ExternalPage] (Topic_PK, Path, Title, URL, Priority, Type, LastUpdated)
VALUES (#1_Topic_PK, #1_Path, #1_Title, #1_URL, #1_Priority, #1_Type, GETDATE());
INSERT INTO [dbo].[XML] (Associated_PK, Type, XMLfragment, LastUpdated)
VALUES (SCOPE_IDENTITY(), N'ExternalPage', #2_XMLfragment, GETDATE());
END
GO

unqiueidenfitier is not compatible with type int SQL Server Procedure

I have the following procedure for inserting into a user table:
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Andy Armstrong
-- Create date:
-- Description:
-- =============================================
CREATE PROCEDURE db_SignupAddLogin
-- Add the parameters for the stored procedure here
#LoginName VARCHAR(15),
#LoginPassword VARCHAR(15)
AS
BEGIN
DECLARE #GUID UNIQUEIDENTIFIER
SET #GUID = NEWID();
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO tblMemberLogin
(
UserID,
LoginName,
LoginPassword
)
VALUES
(
#GUID,
#LoginName,
#LoginPassword
)
RETURN #GUID
END
GO
However when I execute it I get the following error:
Msg 206, Level 16, State 2, Procedure db_SignupAddLogin, Line 34
Operand type clash: uniqueidentifier is incompatible with int
I cannot quite workout why as i am not referencing an int anywhere.
My Schema for tblMemberLogin looks like this:
UserID(PK,uniqueidentifier,notnull)
LoginName(nchar(15),not null)
LoginPassword(nchar(15),not null)
Please help!
RETURN can only be used with an int. You can simply use a SELECT query to retrieve the value of variable #GUID.
Reference: http://technet.microsoft.com/en-us/library/ms174998(v=sql.110).aspx
get rid of RETURN #GUID and you should be good to go.
In SQL Server, stored procedures may only return integer values. SQL Server RETURN
If you want to return data from a stored procedure other than an integer, you can use an output parameter: Returning Data from Stored Procedures
You declare the output parameter along with your input parameters:
CREATE PROCEDURE CREATE PROCEDURE db_SignupAddLogin
-- Add the parameters for the stored procedure here
#LoginName VARCHAR(15),
#LoginPassword VARCHAR(15),
#NewGuid UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
SET #NewGuid = NEWID();
-- rest of procedure
END
And then use the output parameter:
DECLARE #NewLoginGuidFromSP UNIQUEIDENTIFIER
EXECUTE db_SignupAddLogin 'Username', 'password', #NewGuid = #NewLoginGuidFromSP OUTPUT;

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.

Invalid Object Name - Stored Procedure

I am creating a stored procedure in SQL Server via SSMS.
I have written the stored procedure below, however when I click execute it am given the error:
Msg 208, Level 16, State 6, Procedure NewQuestion, Line 11
Invalid object name 'hgomez.NewQuestion'.
the table is ownership is correct. (hgomez.Questions)
USE [devworks_oscar]
GO
/****** Object: StoredProcedure [hgomez].[NewQuestion] Script Date: 10/23/2011 23:55:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [hgomez].[NewQuestion]
(
#QUESTIONNAME nvarchar(50),
#QUESTION_ID int OUTPUT
)
AS
/* SET NOCOUNT ON */
INSERT INTO [Questions] (QuestionText) VALUES (#QUESTIONNAME)
SET #QUESTION_ID = SCOPE_IDENTITY();
RETURN
Thanks in advance
I was a fan of always prepending my CREATE statements with an explicit check for existence and dropping if it was found.
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'NewQuestion' AND ROUTINE_SCHEMA = 'hgomez')
BEGIN
DROP PROCEDURE hgomez.NewQuestion
END
GO
-- this is always a CREATE
CREATE PROCEDURE [hgomez].[NewQuestion]
(
#QUESTIONNAME nvarchar(50),
#QUESTION_ID int OUTPUT
)
AS
/* SET NOCOUNT ON */
INSERT INTO [Questions] (QuestionText) VALUES (#QUESTIONNAME)
SET #QUESTION_ID = SCOPE_IDENTITY();
RETURN
That can be a bit of hassle with regard to permissions so others use an approach wherein they create a stub method only to immediately ALTER it.
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'NewQuestion' AND ROUTINE_SCHEMA = 'hgomez')
BEGIN
EXEC ('CREATE PROCEDURE hgomez.NewQuestion AS SELECT ''stub version, to be replaced''')
END
GO
-- This is always ALTER
ALTER PROCEDURE [hgomez].[NewQuestion]
(
#QUESTIONNAME nvarchar(50),
#QUESTION_ID int OUTPUT
)
AS
/* SET NOCOUNT ON */
INSERT INTO [Questions] (QuestionText) VALUES (#QUESTIONNAME)
SET #QUESTION_ID = SCOPE_IDENTITY();
RETURN
This script tries to modify a procedure that already exists; it doesn't create the procedure.
To create the procedure use CREATE PROCEDURE
CREATE PROCEDURE [hgomez].[NewQuestion]
Once the procedure exists, you can modify its definition by using ALTER PROCEDURE
ALTER PROCEDURE [hgomez].[NewQuestion]
This solution https://stackoverflow.com/a/26775310/2211788 explained
If you drop and re-create a stored procedure it gets a new objectid - the list of stored procedures in SSMS is linked to the id it knows at the time the list was built. If you re-create it but don't refresh the stored procedures folder then any attempts to edit it will indicate the procedure is not found as the id has changed.
This happened to me once when I had two instances of SSMS open and I was working on the one I opened first. Closed them both down, reopened and it worked fine.