In my stored procedure insert into two different tables with if else condition - sql

create procedure sp_student
#student varchar(50);
as
if (#student = marks)
begin
insert into marks
values (student_id int not null, terms varchar(10), subject1 varchar(100), subject2 varchar(100), total varchar(100))
end
else (#student = students)
begin
insert into students
values(student_id int not null, student_name varchar(100), student_age int, mobile_no varchar(20))
end
I didn't get a answer above I mentioned query.

Too many errors. Such a number of errors makes me feel, that you don't really use IBM Db2 product...
create procedure sp_student (
#student varchar(50),
#student_id int,
#terms varchar(10),
#subject1 varchar(100),
#subject2 varchar(100),
#total varchar(100),
#student_name varchar(100),
#student_age int,
#mobile_no varchar(20)
)
BEGIN
if (#student = 'marks') then
insert into marks
values (#student_id, #terms, #subject1 , #subject2 , #total);
elseif (#student = 'students') then
insert into students
values (#student_id ,#student_name, #student_age, #mobile_no);
end if;
END
fiddle

you can't set dataType in value(..) :
create procedure sp_student
#student varchar(50),
#student_id int,
#terms varchar(10),
#subject1 varchar(100),
#subject2 varchar(100),
#total varchar(100),
#student_name varchar(100),
#student_age int,
#mobile_no varchar(20)
as
if (#student = 'marks') then
insert into marks
values (#student_id, #terms, #subject1 , #subject2 , #total);
elseif (#student = 'students') then
insert into students
values (#student_id ,#student_name, #student_age, #mobile_no);
end if;
end

Related

How do I fix the subtotal function correctly and can be inserted into the procedure? I have tried to make it but the result is not suitable

CREATE TABLE product(
codeproduct CHAR(5),
nameproduct VARCHAR(50),
unit VARCHAR(20),
price INT
);
CREATE TABLE producer(
codeproducer CHAR(5),
nameproducer VARCHAR(50),
address VARCHAR(20),
city VARCHAR(20),
province VARCHAR(20)
);
CREATE TABLE transaction(
codeproduct CHAR(5),
codeproducer CHAR(5),
qty INT
);
CREATE FUNCTION [dbo].[subtotal](#codeproduct AS CHAR(5))
RETURNS INT
AS
BEGIN
DECLARE #subtotal INT
SELECT #subtotal = product.price * transaction.qty FROM transaction inner join product
ON product.codeproduct=transaction.codeproduct
WHERE product.codeproduct=#codeproduct
RETURN #subtotal
END
GO
CREATE PROCEDURE showdata
AS
BEGIN
SET NOCOUNT ON;
SELECT producer.nameproducer, product.nameproduct, product.unit, transaction.qty,
product.price, [dbo].[subtotal](product.codeproduct) AS 'subtotal'
FROM product join transaction on product.codeproduct=transaction.codeproduct
join producer on producer.codeproducer=transaction.codeproducer
ORDER BY producer.nameproducer;
END
GO
EXEC showdata

Error converting data type varchar to int.?

My table is below
CREATE TABLE Customers
(
CustomerID int identity(1,1) not null primary key,
Name varchar(50) not null,
PhoneNumber varchar(20) not null
constraint chk_PhoneNumber check(PhoneNumber not like '%[^0-9]%'),
DoorNo varchar(50) not null,
StreetName varchar(50) not null,
City varchar(50) not null,
Statee varchar(50) not null,
Zipcode int not null
)
My stored procedure:
ALTER PROCEDURE stp_customers_insert
(#customerid int,
#name varchar(50),
#phone varchar(50),
#doorno varchar(50),
#streetname varchar(50),
#city varchar(50),
#state varchar(50),
#zip int)
AS
BEGIN
IF EXISTS (SELECT CustomerID FROM Customers WHERE CustomerID = #customerid)
BEGIN
RAISERROR ('employee id already exists', 1, 1)
END
ELSE
BEGIN
INSERT INTO Customers (Name, PhoneNumber, DoorNo, StreetName, City, Statee, Zipcode)
VALUES (#name, #phone, #doorno, #streetname, #city, #state, #zip)
END
END
Sample call:
exec stp_customers_insert 'ram', '674673932', '122', '5th cross', 'trichy', 'tamilnadu', 620001
I get this error:
Msg 8114, Level 16, State 5, Procedure stp_customers_insert, Line 23
Error converting data type varchar to int.
The problem appears to be that your stored procedure expects 8 parameters:
stp_customers_insert(#customerid int, #name varchar(50), #phone varchar(50),
#doorno varchar(50), #streetname varchar(50), #city varchar(50),
#state varchar(50), #zip int)
but you are only passing 7 parameters when you actually call the proc:
exec stp_customers_insert 'ram','674673932','122','5th cross','trichy','tamilnadu',620001
If you don't know or don't want to perform the duplicate check on the CustomerID, then you could slightly modify your call to just pass NULL:
exec stp_customers_insert NULL, 'ram','674673932','122','5th cross','trichy','tamilnadu',620001
As an aside, if the proc is not even inserting the CustomerID, and this field is auto increment, then I don't see the point of passing it. Instead, you might want to consider using a unique constraint to achieve the same.
exec stp_customers_insert 1,'ram','674673932','122','5thcross','trichy','tamilnadu',620001
You have to pass #customerid value in procedure parameters - then it will execute without error.
In your table structure CustomerID is defined as INT. But as your stored procedure is defined in this format:
stp_customers_insert(#customerid int,#name ....
You are sending ram as value for customerid in
exec stp_customers_insert 'ram','674673932'....
Correct this to:
exec stp_customers_insert '*enter the CustId value here*','ram','674673932'....
Replace *enter the CustId value here* with the CustomerID
Also change:
insert into Customers(Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(#name,#phone,#doorno,#streetname,#city,#state,#zip)
To include CustomerID as:
insert into Customers(CustomerID,Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(#customerid,#name,#phone,#doorno,#streetname,#city,#state,#zip)
I suggest remove #customerid from sp CustomerID is auto increment field so no need to pass any value for CustomerID. It should be like this:
alter procedure stp_customers_insert(#name varchar(50),#phone varchar(50),#doorno varchar(50),#streetname varchar(50),#city varchar(50),#state varchar(50),#zip int)
as
begin
if exists(select CustomerID from Customers where Name = #name ,phone = #phone ,doorno = #doorno ,streetname = #streetname ,city= #city,state= #state , zip = #zip )
begin
raiserror('employee id already exists',1,1)
end
else
begin
insert into Customers(Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(#name,#phone,#doorno,#streetname,#city,#state,#zip)
end
end
exec stp_customers_insert 'ram','674673932','122','5th cross','trichy','tamilnadu',620001

Why Ident_Current returns null but Scope_Identity does?

It returns null, why? The IDENT_CURRENT part but it works well with Scope_Identity, why ?
ALTER PROCEDURE [dbo]. [InsertComplaints]
#ComplaintCode varchar(50),
#ComplaintType_ID smallint,
#RecievingMode_ID smallint,
#Subject varchar(100),
#ComplainantID smallint,
#District_ID smallint,
#AddressedTo varchar(50),
#DiaryNo varchar(50),
#User_ID int,
#Status_ID smallint,
#RecievedDate smalldatetime,
#IGRemarks varchar(MAX) = null,
#PsoRemarks varchar(MAX) =null,
#FinalDecision varchar(250)=null,
#AgainstDist_ID smallint,
#HomePS_ID smallint,
#AgainstPS_ID smallint,
#Name varchar(75),
#DesigID int,
#ForwardedBy smallint,
#SMS_ID int = 0,
#result bit output,
#ID int output
AS
BEGIN
Begin Try
insert into dbo.Complaints
values (
#ComplaintCode,
#ComplaintType_ID,
#RecievingMode_ID ,
#Subject,
#ComplainantID ,
#District_ID ,
#AddressedTo ,
#DiaryNo,
#User_ID,
#Status_ID,
#RecievedDate,
#IGRemarks,
#PsoRemarks,
#FinalDecision,
#AgainstDist_ID,
#HomePS_ID,
#AgainstPS_ID,
#Name ,
#DesigID,
#ForwardedBy,
#SMS_ID
)
Set #result = ##ROWCOUNT
Set #ID = IDENT_CURRENT('ComplaintID') --SCOPE_IDENTITY()
Select #ID
End Try
Begin Catch
Set #result=0
End Catch
END
I want to get a last inserted id from particular table such that complaintID from complaints table but it doesn't return any but null. Help !
According to msdn the IDENT_CURRENT function will return null if the calling user does not have enough permissions on this object.
Another possibility is that your object name is wrong, i see you're doing your insert with the dbo prefix. Maybe that will help.

updating nulls value in sql server

how do i set a column value to null if the user don't select value or set the new value when the user select value
create procedure update2
#SSN int,
#Employee_FirstName varchar(50),
#Employee_lastName varchar(50),
#gender_ID int,
#Department_ID int,
#location_ID int,
#salary decimal(18, 3)
as
Update Employee set Employee_FirstName= #Employee_FirstName,
Employee_lastName=#Employee_lastName,gender=#gender_ID,Department_ID = #Department_ID,
location_ID=#location_ID or location_ID is null , salary=#salary
where SSN = #SSN
You can declare #location_ID as null
CREATE PROCEDURE update2
#SSN INT ,
#Employee_FirstName VARCHAR(50) ,
#Employee_lastName VARCHAR(50) ,
#gender_ID INT ,
#Department_ID INT ,
#location_ID INT = NULL ,
#salary DECIMAL(18, 3)
AS
UPDATE Employee
SET Employee_FirstName = #Employee_FirstName ,
Employee_lastName = #Employee_lastName ,
gender = #gender_ID ,
Department_ID = #Department_ID ,
location_ID = #location_ID ,
salary = #salary
WHERE SSN = #SSN
Try this one, If I correctly understood your question -
CREATE PROCEDURE dbo.update2
#SSN INT,
#Employee_FirstName VARCHAR(50),
#Employee_lastName VARCHAR(50),
#gender_ID INT,
#Department_ID INT,
#location_ID INT,
#salary DECIMAL(18, 3)
AS BEGIN
UPDATE dbo.Employee
SET
Employee_FirstName = #Employee_FirstName
, Employee_lastName = #Employee_lastName
, gender = #gender_ID
, Department_ID = #Department_ID
, location_ID = ISNULL(location_ID, #location_ID) --<-- ??
, salary = #salary
WHERE SSN = #SSN
END
#location_ID INT = NULL -- specify NULL like this wherever you need
But you have to remove any relations(foreign key) with this particular column otherwise you will get conflict occur error at the time of updation
Thanks
Arun Antony

Syntax issue in my SQL Server Stored Procedure with a BIT value type

I am trying to insert an xmlString value and bit value of 1 into the following stored procedure:
ALTER PROCEDURE [dbo].[sbssp_ArchivedMessages]
(
#xmlString varchar(max),
#fromToMach bit
)
AS
BEGIN
DECLARE #idoc int, #lastId int
EXEC sp_xml_preparedocument #idoc OUTPUT, #xmlString, #fromToMach
INSERT INTO [dbo].[tblArchivedMessages]
SELECT * FROM OPENXML(#idoc, '/Mach', 2) WITH [dbo].[tblArchivedMessages]
SET #lastId = (SELECT IDENT_CURRENT('tblArchivedMessages'))
UPDATE [dbo].[tblArchivedMessages]
SET FromToMach = #fromToMach
WHERE ID = #lastId
END
When I execute the SP I am doing so as follows:
EXEC dbo.sbssp_InsertArchivedMessages '<MACH><B000>StringVal</B000><B002>StringVal</B002><B003>StringVal</B003><B004>StringVal</B004><B007>StringVal</B007><B011>StringVal</B011><B012>StringVal</B012><B013>StringVal</B013><B015>StringVal</B015><B018>StringVal</B018><B028>StringVal</B028><B032>StringVal</B032><B037>StringVal</B037><B039>StringVal</B039><B041>StringVal</B041><B043>StringVal</B043><B048>StringVal</B048><B049>StringVal</B049><B058>StringVal</B058><B061>StringVal</B061><B063>StringVal</B063><B127>StringVal</B127></MACH>', 1
This is the error I'm getting:
Msg 515, Level 16, State 2, Procedure sbssp_InsertArchivedMessages,
Line 13
Cannot insert the value NULL into column 'FromToMach', table
'MachoPOSt.dbo.ArchivedMessages'; column does not allow nulls. INSERT
fails. The statement has been terminated.
Any suggestions on what exactly I'm doing wrong? I've also put the #variable names in front of each value but still get the same error.
Any help/direction would be appreciated. Thanks.
You are not populating one of the columns
I am not sure what the table looks like but the problem is this
INSERT INTO [dbo].[tblArchivedMessages]
SELECT * FROM OPENXML(#idoc, '/ATM', 2) WITH [dbo].[tblArchivedMessages]
The problem is the value that you pass into the FromToATM column
#fromToMatch shouldn't be supplied as the last parameter of the sp_xml_preparedocument stored procedure.
That param is for an xPath Namespace, which you do not appear to have any of in your sample XML, so there is no value required for this parameter in this situation.
Also, XML is CASE SensiTive, so in your OPENXML, change "Mach" to "MACH".
Without having the DDL for any of the tables involved, I can't accurately simulate it on my system, but give this a try:
EXEC sp_xml_preparedocument
#idoc OUTPUT,
#xmlString;
INSERT INTO [dbo].[tblArchivedMessages]
--(really should list columns here)
SELECT
--really should list columns here
--instead of
*
FROM OPENXML(#idoc, '/MACH', 2)
WITH [dbo].[tblArchivedMessages];
SET #lastId = SCOPE_IDENTITY();
EXEC sp_xml_removedocument
#idoc; --never a bad idea to clean up!
UPDATE [dbo].[tblArchivedMessages] SET
FromToMach = #fromToMach
WHERE
ID = #lastId;
Give'er a try, see if it works.
If not, script your "tblArchivedMessages" table, and post the DDL.
We'll get it working, one way or the other!
Cheers!
(Incidentally, this code DOES appear to work as is on my machine):
DECLARE
#idoc int,
#lastId int,
#xmlString varchar(max),
#fromToMach bit
SET #xmlString =
'<MACH><B000>StringVal</B000><B002>StringVal</B002><B003>StringVal</B003><B004>StringVal</B004><B007>StringVal</B007><B011>StringVal</B011><B012>StringVal</B012><B013>StringVal</B013><B015>StringVal</B015><B018>StringVal</B018><B028>StringVal</B028><B032>StringVal</B032><B037>StringVal</B037><B039>StringVal</B039><B041>StringVal</B041><B043>StringVal</B043><B048>StringVal</B048><B049>StringVal</B049><B058>StringVal</B058><B061>StringVal</B061><B063>StringVal</B063><B127>StringVal</B127></MACH>';
SET #fromToMach = 1;
EXEC sp_xml_preparedocument
#idoc OUTPUT,
#xmlString;
SELECT
--really should list columns here
--instead of
*
FROM OPENXML(#idoc, '/MACH', 2)
WITH
(
B000 VARCHAR(100),
B001 VARCHAR(100),
B002 VARCHAR(100),
B003 VARCHAR(100),
B004 VARCHAR(100),
B005 VARCHAR(100),
B006 VARCHAR(100),
B007 VARCHAR(100),
B008 VARCHAR(100),
B009 VARCHAR(100),
B010 VARCHAR(100),
B011 VARCHAR(100),
B012 VARCHAR(100),
B013 VARCHAR(100),
B014 VARCHAR(100),
B015 VARCHAR(100),
B016 VARCHAR(100),
B017 VARCHAR(100),
B018 VARCHAR(100),
B019 VARCHAR(100),
B020 VARCHAR(100),
B021 VARCHAR(100),
B022 VARCHAR(100),
B023 VARCHAR(100),
B024 VARCHAR(100),
B025 VARCHAR(100),
B026 VARCHAR(100),
B027 VARCHAR(100),
B028 VARCHAR(100),
B029 VARCHAR(100),
B030 VARCHAR(100),
B031 VARCHAR(100),
B032 VARCHAR(100),
B033 VARCHAR(100),
B034 VARCHAR(100),
B035 VARCHAR(100),
B036 VARCHAR(100),
B037 VARCHAR(100),
B038 VARCHAR(100),
B039 VARCHAR(100),
B040 VARCHAR(100),
B041 VARCHAR(100),
B042 VARCHAR(100),
B043 VARCHAR(100),
B044 VARCHAR(100),
B045 VARCHAR(100),
B046 VARCHAR(100),
B047 VARCHAR(100),
B048 VARCHAR(100),
B049 VARCHAR(100),
B050 VARCHAR(100),
B051 VARCHAR(100),
B052 VARCHAR(100),
B053 VARCHAR(100),
B054 VARCHAR(100),
B055 VARCHAR(100),
B056 VARCHAR(100),
B057 VARCHAR(100),
B058 VARCHAR(100),
B059 VARCHAR(100),
B060 VARCHAR(100),
B061 VARCHAR(100),
B062 VARCHAR(100),
B063 VARCHAR(100),
B064 VARCHAR(100),
B065 VARCHAR(100),
B066 VARCHAR(100),
B067 VARCHAR(100),
B068 VARCHAR(100),
B069 VARCHAR(100),
B070 VARCHAR(100),
B071 VARCHAR(100),
B072 VARCHAR(100),
B073 VARCHAR(100),
B074 VARCHAR(100),
B075 VARCHAR(100),
B076 VARCHAR(100),
B077 VARCHAR(100),
B078 VARCHAR(100),
B079 VARCHAR(100),
B080 VARCHAR(100),
B081 VARCHAR(100),
B082 VARCHAR(100),
B083 VARCHAR(100),
B084 VARCHAR(100),
B085 VARCHAR(100),
B086 VARCHAR(100),
B087 VARCHAR(100),
B088 VARCHAR(100),
B089 VARCHAR(100),
B090 VARCHAR(100),
B091 VARCHAR(100),
B092 VARCHAR(100),
B093 VARCHAR(100),
B094 VARCHAR(100),
B095 VARCHAR(100),
B096 VARCHAR(100),
B097 VARCHAR(100),
B098 VARCHAR(100),
B099 VARCHAR(100),
B100 VARCHAR(100),
B101 VARCHAR(100),
B102 VARCHAR(100),
B103 VARCHAR(100),
B104 VARCHAR(100),
B105 VARCHAR(100),
B106 VARCHAR(100),
B107 VARCHAR(100),
B108 VARCHAR(100),
B109 VARCHAR(100),
B110 VARCHAR(100),
B111 VARCHAR(100),
B112 VARCHAR(100),
B113 VARCHAR(100),
B114 VARCHAR(100),
B115 VARCHAR(100),
B116 VARCHAR(100),
B117 VARCHAR(100),
B118 VARCHAR(100),
B119 VARCHAR(100),
B120 VARCHAR(100),
B121 VARCHAR(100),
B122 VARCHAR(100),
B123 VARCHAR(100),
B124 VARCHAR(100),
B125 VARCHAR(100),
B126 VARCHAR(100),
B127 VARCHAR(100)
)
SET #lastId = SCOPE_IDENTITY();
EXEC sp_xml_removedocument
#idoc; --never a bad idea to clean up!