sql server, procedures and function - sql-server-2008

I have a Studentstatus table which gets a list of students from student table.
I made this procedure to update or create a new record, but I get the following error:
Procedure or function SaveAdvStudStata has too many arguments specified.
CREATE PROCEDURE [dbo].[SaveAdvStudStata]
#studentId int,
#stata int,
#description varchar(MAX),
#date DATE,
#issuedBy nvarchar(128)
AS
BEGIN
if exists (select StudentId
from dbo.StudentStata
where StudentId = #studentId
)
begin
Update dbo.StudentStata
set Stata = #stata
,Description = #description
,Date = #date
,IssuedBy = #issuedBy
where StudentId = #studentId
end
else
begin
Insert into dbo.StudentStata(StudentId
,Stata
,Description
,Date
,IssuedBy
)
VALUES (#studentId
,#stata
,#description
,#date
,#issuedBy
)
end
END

Related

SQL - Procedure function

Trying to create a Procedure to (Insert, Delete and, Update) values in employee_details.
CREATE DEFINER=`root`#`localhost` ALTER PROCEDURE `alter_employeedetails`(in employee_id int(11), employee_name VARCHAR(30), employee_join_date date,
employee_desgination varchar(30), employee_salary bigint(20), employee_address varchar(30),
employee_contact varchar(30), employee_email_id varchar(30)
BEGIN
IF #StatementType = 'Insert'
BEGIN
insert into employee_details values
(employee_id, employee_name, employee_join_date, employee_desgination, employee_salary, employee_address, employee_contact, employee_email_id)
END
IF #StatementType = 'Update'
BEGIN
UPDATE employee_details SET
(employee_name = #employee_name, employee_join_date = #employee_join_date, employee_designation = #employee_desgination,
employee_salary = #employee_salary, employee_address = #employee_address, employee_contact = #employee_contact, employee_email_id = #employee_email_id)
WHERE employee_id = #employee_id
END
else IF #StatementType = 'Delete'
BEGIN
DELETE FROM employee_details where employee_id = #employee_id
END
end
Quite a few errors in that code...
You forgot to prefix all the parameters with the "#" symbol.
Forgot to include "#StatementType" as a parameter.
Update had brackets around it.
You cannot specify int(11) (employee_id) or bigint(20)
(salary). It's either int / bigint (you don't specify the length
for int/bigint datatypes). And is salary correct as bigint? MSSQL has a "money"
datatype, or you could use decimal(8,2) or something similar. You
might be multiplying the salary by 100 to shift the decimal place for
all I know?
When inserting, do you really want to insert a employee Id? This would normally be an auto-incrementing primary key
Insert statement missing the fields you were populating. Required if using the "values" keyword like you had specified.
Hopefully this is closer to what you want.
ALTER PROCEDURE alter_employeedetails
(#StatementType as varchar(25), #employee_id int, #employee_name VARCHAR(30), #employee_join_date date,
#employee_designation varchar(30), #employee_salary bigint, #employee_address varchar(30),
#employee_contact varchar(30), #employee_email_id varchar(30))
AS
BEGIN
IF #StatementType = 'Insert'
BEGIN
insert into employee_details
(employee_id, employee_name, employee_join_date, employee_designation, employee_salary, employee_address, employee_contact, employee_email_id)
values
(#employee_id, #employee_name, #employee_join_date, #employee_designation, #employee_salary, #employee_address, #employee_contact, #employee_email_id)
END
ELSE IF #StatementType = 'Update'
BEGIN
UPDATE employee_details
SET
employee_name = #employee_name,
employee_join_date = #employee_join_date,
employee_designation = #employee_designation,
employee_salary = #employee_salary,
employee_address = #employee_address,
employee_contact = #employee_contact,
employee_email_id = #employee_email_id
WHERE employee_id = #employee_id
END
ELSE IF #StatementType = 'Delete'
BEGIN
DELETE FROM employee_details where employee_id = #employee_id
END
END

i want to insert same record for status=0 for the below code in mysql. but my logic is not working.can any one suggest me

I want to insert same record for status=0. But my logic is not working in MySQL.
Please add more text over here...
This question is about a stored procedure. And the user didn't provide any info what so ever.
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_insert2`(
IN P_page_Id int(11) ,
IN P_LanguageCode char,
IN P_page_status int(11),
IN p_created_by int(11),
IN p_created_date datetime,
OUT P_return int)
BEGIN
DECLARE v_isRecordExisting int;
DECLARE v_flags int;
SELECT
COUNT(*)
INTO
v_isRecordExisting
FROM
tbl_cms_pages_languages2
WHERE
page_status in(0,1)
AND
page_id = p_page_Id
AND
languagecode = P_LanguageCode;
IF
v_isRecordExisting = 0
THEN
INSERT INTO tbl_cms_pages_languages2 (
page_Id,
LanguageCode,
page_status,
created_by,
created_date
)
VALUES(
P_page_Id,
P_LanguageCode,
P_page_status,
P_created_by,
p_created_date
);
SET
p_return = 0;
ELSE
SET
p_return = 2;
END IF;
END

Use of transaction in stored procedure

I create a stored procedure and I want to use transaction in this. So how can I use transaction in this stored procedure? Which part will start the transaction because INSERT and UPDATE both are together in this stored procedure.
This is my stored procedure:
ALTER PROCEDURE [dbo].[usp_InsertUpdateADAlertRecipient]
#AD_User_ID int,
#AD_Client_ID int,
#AD_Org_ID int,
#IsActive bit,
#Created datetime,
#Createdby int,
#Updated datetime,
#Updatedby int,
#AD_Alertrecipient_ID int,
#AD_AlertRule_ID int,
#AD_Role_ID int,
#Sendemail bit
AS
SET NOCOUNT ON
IF EXISTS(SELECT [AD_Alertrecipient_ID] FROM [dbo].[AD_AlertRecipient] WHERE [AD_Alertrecipient_ID] = #AD_Alertrecipient_ID)
BEGIN
UPDATE [dbo].[AD_AlertRecipient] SET
[AD_User_ID] = #AD_User_ID,
[AD_Client_ID] = #AD_Client_ID,
[AD_Org_ID] = #AD_Org_ID,
[IsActive] = #IsActive,
[Created] = #Created,
[Createdby] = #Createdby,
[Updated] = #Updated,
[Updatedby] = #Updatedby,
[AD_AlertRule_ID] = #AD_AlertRule_ID,
[AD_Role_ID] = #AD_Role_ID,
[Sendemail] = #Sendemail
WHERE
[AD_Alertrecipient_ID] = #AD_Alertrecipient_ID
select #AD_Alertrecipient_ID
END
ELSE
BEGIN
INSERT INTO [dbo].[AD_AlertRecipient] (
[AD_User_ID],
[AD_Client_ID],
[AD_Org_ID],
[IsActive],
[Created],
[Createdby],
[Updated],
[Updatedby],
--[AD_Alertrecipient_ID],
[AD_AlertRule_ID],
[AD_Role_ID],
[Sendemail]
) VALUES (
#AD_User_ID,
#AD_Client_ID,
#AD_Org_ID,
#IsActive,
#Created,
#Createdby,
#Updated,
#Updatedby,
--#AD_Alertrecipient_ID,
#AD_AlertRule_ID,
#AD_Role_ID,
#Sendemail
)
SELECT SCOPE_IDENTITY()
END
Thanks and waiting for your reply .

I am getting error# 1064 while trying to execute the following, help me to solve it

I need to check boolean variable dynamic_select, if it is 1, then I have to insert the corresponding equipments from the equipments table
CREATE DEFINER=`root`#`localhost` PROCEDURE
`sp_CONTRACT_EQUIPMENTS( P_CONTRACT_EQUIPMENT_ID INT,
P_CONTRACT_ID INT,
P_CATEGORY_ID INT,
P_DYNAMIC_SELECT TINYINT )`
BEGIN
DECLARE V_DYNAMIC_SELECT TINYINT;
DECLARE V_EQUIPMENT_ID INT;
SELECT DYNAMIC_SELECT INTO V_DYNAMIC_SELECT FROM CONTRACT_EQUIPMENTS
WHERE CONTRACT_ID=P_CONTRACT_ID;
SELECT EQUIPMENT_ID INTO V_EQUIPMENT_ID FROM EQUIPMENT E
INNER JOIN CONTRACT_CATEGORY CC
ON E.CONTRACT_ID = CC.CONTRACT_ID
WHERE CC.CATEGORY_ID = P_CATEGORY_ID;
IF V_DYNAMIC_SELECT=1 THEN
IF NOT EXISTS
(SELECT EQUIPMENT_ID FROM CONTRACT_EQUIPMENTS CE1
WHERE CE1.EQUIPMENT_ID=E.EQUIPMENT_ID AND
CE1.CONTRACT_ID=P_CONTRACT_ID) THEN
INSERT
INTO `CONTRACT_EQUIPMENTS`(CONTRACT_EQUIPMENT_ID,CONTRACT_ID,EQUIPMENT_ID,DYNAMIC_SELECT)
VALUES(P_CONTRACT_EQUIPMENT_ID,P_CONTRACT_ID,V_EQUIPMENT_ID,0);
END IF;
END IF;
END
Back ticks ( ` ) around procedure name are not required.
This is wrong:
`sp_CONTRACT_EQUIPMENTS(P_CONTRACT_EQUIPMENT_ID INT,
P_CONTRACT_ID INT,P_CATEGORY_ID INT,P_DYNAMIC_SELECT TINYINT)`
Change to:
sp_CONTRACT_EQUIPMENTS( P_CONTRACT_EQUIPMENT_ID INT,
P_CONTRACT_ID INT, P_CATEGORY_ID INT, P_DYNAMIC_SELECT TINYINT )

update and insert action in stored procedure

Can you tell me what is the wrong? I want to have a stored procedure that can do both update and insert action.
CREATE PROCEDURE `save_user`(IN `sp_aliasName` VARCHAR(100),IN `sp_password` VARCHAR(100), IN `sp_sex` VARCHAR(100), IN `sp_age` INT(11), IN `sp_userGroup` VARCHAR(100),OUT `sp_number_of_user` INT)
BEGIN
DECLARE temp INT;
SELECT COUNT(user_id) INTO sp_number_of_user FROM user WHERE aliasName = sp_aliasname;
temp := sp_number_of_user;
IF temp > 0
THEN
UPDATE user SET aliasName=sp_aliasName,password=sp_password,sex=sp_sex,age=sp_age,userGroup=sp_userGroup
WHERE aliasName = sp_aliasname ;
ELSE
INSERT INTO user (aliasName,password,sex,age,userGroup)
values (sp_aliasName,sp_password,sp_sex,sp_age,sp_userGroup);
END IF;
END
I think your procedure should look like this -
CREATE PROCEDURE `save_user`(IN `sp_aliasName` VARCHAR(100),IN `sp_password` VARCHAR(100), IN `sp_sex` VARCHAR(100), IN `sp_age` INT(11), IN `sp_userGroup` VARCHAR(100),OUT `sp_number_of_user` INT)
BEGIN
DECLARE temp INT;
SELECT count(user_id) INTO sp_number_of_user FROM user WHERE aliasName = sp_aliasname;
SET temp := sp_number_of_user;
IF temp > 0 THEN
UPDATE user SET aliasName = sp_aliasName, password = sp_password, sex = sp_sex, age = sp_age, userGroup = sp_userGroup
WHERE aliasName = sp_aliasname;
ELSE
INSERT INTO user (aliasName, password, sex, age, userGroup) VALUES (sp_aliasName, sp_password, sp_sex, sp_age, sp_userGroup);
END IF;
END
I added:
INT type to OUT parameter
WHERE clause to UPDATE statement (check this condition)
Also, have a look at INSERT ... ON DUPLICATE KEY UPDATE command, it may do what you need.