Error when trying to create stored procedure - sql-server

I am trying to create a procedure in my SQL Server Management Studio.
I wrote this code:
CREATE PROCEDURE [dbo].[InsertBookDetails_Sp]
#BookName VARCHAR(100),
#Author VARCHAR(100),
#Publisher VARCHAR(100),
#Price DECIMAL(18,2),
#BookPic VARBINARY(MAX) = NULL,
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO BookDetails(BookName, Author, Publisher, Price, BookPic)
VALUES (#BookName, #Author, #Publisher, #Price, #BookPic)
END
but it shows error
Incorrect syntax near 'As'.
Invalid ObjectName BookDetails
Invalid Column name BookName
Invalid Column name Author
Invalid Column name Publisher
Invalid Column name Price
Invalid Column name BookPic
How to solve this error?

The last parameter should not have ",". Remove the extra "," and try:
CREATE PROCEDURE [dbo].[InsertBookDetails_Sp]
#BookName VARCHAR(100),
#Author VARCHAR(100),
#Publisher VARCHAR(100),
#Price DECIMAL(18, 2),
#BookPic VARBINARY(MAX) = NULL
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO BookDetails
(BookName, Author, Publisher, Price, BookPic)
VALUES
(#BookName, #Author, #Publisher, #Price, #BookPic);
END

problem solved by removing comma near As

Related

i'm trying to create trigger in sql server and i'm getting error "Incorrect syntax near the keyword 'user'."

i want to insert data in user from table Aspnetusers but i'm getting this error Incorrect syntax near the keyword 'user' i don't know where is the problem with the code
create trigger insertdata
ON AspNetUsers
After Insert
as
begin
declare
#userid int,
#userfname nvarchar(50),
#userlname nvarchar(50),
#Usermail nvarchar(50)
select #userid=ClientID,#userfname=FirstName,#userlname=LastName,#Usermail=UserName from AspNetUsers
insert into user values(#userid,#userfname,#userlname,#Usermail)
end
go
create trigger insertdata
ON final
After Insert
as
begin
declare
#userid int,
#userfname nvarchar(50),
#userlname nvarchar(50),
#Usermail nvarchar(50)
select #userid=ClientID,#userfname=FirstName,#userlname=LastName,#Usermail=UserName from AspNetUsers
insert into [user] values(#userid,#userfname,#userlname,#Usermail)
end
go

Column name or number of supplied values does not match table definition using sql server

In SQL server, I am trying to creating stored Procedure by using the below query:
Create procedure [dbo].[AddNewEmpDetails]
(
#Name varchar (50),
#City varchar (50),
#Address varchar (50)
)
as
begin
Insert into Employee values(#Name,#City,#Address)
End
How can i resolve this issue.
Probably your Employee table has more than three columns but you supplied here only three.
For resolution, you should mention all the columns value or mention the name of these columns in the insert statement. The second solution will work if the rest columns can accept null values. For Example
Insert into Employee (Name, City, Address) values (#Name, #City, #Address)

Invalid object name, and invalid column name

I am getting the Invalid object name 'CUSTOMER_temp'. Error code. I made these two tables and made the temporary table. Also when I put state from inserted. It gives me the error code Invalid column name 'state'. I am not sure if I need this still or if I am able to get rid of it.
The purpose of this trigger is to automatically copy new records to a new table.
DROP TABLE CUSTOMER
CREATE TABLE CUSTOMER
(
CustomerID CHAR(5) PRIMARY KEY, --Make Primary Key
CustLastName VARCHAR(20),
CustFirstName VARCHAR(20),
CustStreet VARCHAR(60),
CustCity VARCHAR(30),
CustState CHAR(2),
CustZip CHAR(5),
CustPhone CHAR(10),
CustEmail CHAR(50),
);
drop table CUSTOMER_temp
CREATE TABLE CUSTOMER_temp -- temporary table
(
CustomerID CHAR(5) PRIMARY KEY,
CustLastName VARCHAR(20),
CustFirstName VARCHAR(20),
CustStreet VARCHAR(60),
CustCity VARCHAR(30),
CustState CHAR(2),
CustZip CHAR(5),
CustPhone CHAR(10),
CustEmail CHAR(50),
);
CREATE TRIGGER dbo.CustCopy
On CUSTOMER
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
insert CUSTOMER_temp (CustomerID, CustLastName, CustPhone, CustState)
Select CustomerID, CustLastName, CustPhone, CustState from inserted
END
Try this... You need INTO after the Insert.
UPDATE removed dbo.
CREATE TRIGGER dbo.CustCopy
On CUSTOMER
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
insert INTO CUSTOMER_temp (CustomerID, CustLastName, CustPhone, CustState)
Select CustomerID, CustLastName, CustPhone, CustState from inserted
END

Create a table with current date appended to table name in Azure

I know that this is a terrible practice but this is what I am being asked for. This procedure will be executed about once a month but that could change. I need the format of the new table name to be staff.20150818 which is staff.yyyymmdd. When I run my procedure, the table name is #currentDate instead of what I need it to be. In SQL Azure, I cannot use PREPARE which has been a factor in many of the solutions I have found. Here is the code I have been working on:
BEGIN
DECLARE #currentDate varchar(500);
SET #currentDate = 'staff.' +(CONVERT(VARCHAR(8),GETDATE(),3));
CREATE TABLE [dbo].[#currentDate] (
[staffID] int identity(1,1) primary key,
[firstName] nvarchar(35),
[lastName] nvarchar(35),
[positionTitle] nvarchar(45),
[leaID] nvarchar(15),
[schoolID] nvarchar(15),
[phoneNumber] nvarchar(24),
[email] nvarchar(128),
[username] nvarchar(20),
[password] nvarchar(max),
[code1] nvarchar(5),
[code2] nvarchar(5),
[date_created] datetime2(7),
[date_updated] datetime2(7)
) INSERT INTO [#currentDate](firstName, lastName, positionTitle, leaID, schoolID, phoneNumber, email, username, password, code1, code2)
SELECT firstName, lastName, positionTitle, leaID, schoolID, phoneNumber, email, username, password, code1, code2
FROM Staff
END
You just have to use dynamic SQL, Concat your SQL statement into a string, including converting the date time to varchar, then call EXEC or sp_executeSql on it.
You can't pass the table name as a variable to sp_executeSql; you need to have already resolved that in the #sql string.
Have you tried using dynamic sql?
Something like this should work :
EXECUTE sp_executesql
N'CREATE TABLE [dbo].[#currentDate] (
[staffID] int identity(1,1) primary key,
...',
N'#currentDate varchar(500)',
#currentDate = 'staff.' +(CONVERT(VARCHAR(8),GETDATE(),3))
Documentation on sp_executesql

Can I insert into temp table from SP without declaring table?

Here is my code:
alter procedure test1 as
select DeptID,DeptName from Department
go
alter procedure test2 as
--Create Table #tab (DeptID INT, DeptName VARCHAR(255))
INSERT INTO #tab
exec test1
select * from #tab
drop table #tab
go
exec test2
I am getting an error like "Invalid object name #tab"
If I add at the begining Create Table #tab (DeptID INT, DeptName VARCHAR(255)) then I do not get any error.
What is wrong in my code? Can I populate a temp table from the results of a stored procedure without declaring the temp table and its column definitions?
When loading a temp table from a stored procedure, then you have to CREATE the table first.
There is no straightforward equivalent of
SELECT * INTO #temptable FROM AnotherTable
The non-straightforward version (read all about the bad stuff on "How to Share Data Between Stored Procedures". And simpler) would be
SELECT * INTO #temptable FROM OPENQUERY(Loopback, 'exec test1')
It's because the Local Temporary table #tab which you are expecting does not existing in the session.
So the table creation should not be commented line.
Create Table #tab (DeptID INT, DeptName VARCHAR(255))
Moreover, if you want to do without creating the table then it should be like below
Alter procedure test2
As
Set NoCount ON
IF OBJECT_ID('tempdb..#tab') IS NOT NULL
Begin
Drop table #temp
End
SELECT DeptID, DeptName INTO #tab from Department
Select * from #tab
Drop table #tab
Go

Resources