Click here to Skip to main content
15,890,409 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
Hi,
I am able to perform insert and update from the below query but cant select the records, where and how to write the select query in this procedure.
SQL
alter PROCEDURE usp_sel_DailyReport
@Id int = null,  
@WithdrawalAmount int,  
@Balance int,  
@BankName varchar(30),
@WorkerLabour varchar(50),
@PurchaseMatrialAmount int,
@PurchaseAgainst varchar(50),
@Comments varchar(max)
as begin
if(@ID is null)  
insert into tbldailyreport (WithdrawalAmount,Balance,BankName,WorkerLabour,PurchaseMatrialAmount,PurchaseAgainst,Comments,IsActive,Date ,CreatedBy ,LastUpdatedBy)
values (@WithdrawalAmount,@Balance,@BankName,@WorkerLabour,@PurchaseMatrialAmount,@PurchaseAgainst,@Comments,1,Getdate(),'Admin','Admin')  
else
update tbldailyreport set WithdrawalAmount=@WithdrawalAmount,Balance=@Balance,BankName=@BankName,WorkerLabour=@WorkerLabour,PurchaseMatrialAmount=@PurchaseMatrialAmount,PurchaseAgainst=@PurchaseAgainst,Comments=@Comments,Date = GetDate()
where ID=@ID
end

Where do i write the select query in the same procedure which is used in data binding
SQL
select Id,WithdrawalAmount,Balance,BankName,WorkerLabour,PurchaseMatrialAmount,PurchaseAgainst,Comments from tbldailyreport where IsActive=1  
Posted

Just write a new SP and bind that into your data binding.

Sample Select SP is as below :

SQL
/*  Getstudentname is the name of the stored procedure*/

Create  PROCEDURE Getstudentname(

@studentid INT                   --Input parameter ,  Studentid of the student

)
AS
BEGIN
SELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid
END


For more info check this : Sql Server - How to write a Stored procedure in Sql server[^]

UPDATE

You can use something like below.But there're pros and cons in this way.For more info about this check below mentioned article.

CREATE PROCEDURE [dbo].[spTABLENAME]
@Function nvarchar = null,
@ID int = null,
@MoreVariables int = null

AS
BEGIN
    SET NOCOUNT ON;

IF @Function = 'UPDATE'
    BEGIN
        UPDATE UPDATESTUFF WHERE ID = @ID;
    END
ELSE IF @Function = 'INSERT'
    BEGIN
        INSERT INTO TABLENAME (STUFF)
    END
ELSE IF @Function = 'SELECT'
    BEGIN
        SELECT * FROM TABLENAME  WHERE ID= @ID
    END
ELSE IF @Function = 'DELETE'
    BEGIN
        DELETE * FROM TABLENAME WHERE ID = @ID  
    END


For more info: SOF

I hope this will help to you.
 
Share this answer
 
v3
Comments
Member 9176543 27-Oct-13 11:22am    
Hi,
I know that a new SP can be written but there would be a way to achieve this in a single SP,I want to know that.
Sampath Lokuge 27-Oct-13 11:31am    
Plz check my 'UPDATE' section.
You can try below like this as you don't want to use any other Parameter

SQL
alter PROCEDURE usp_sel_DailyReport
@Id int = null,  
@WithdrawalAmount int,  
@Balance int,  
@BankName varchar(30),
@WorkerLabour varchar(50) = Null,
@PurchaseMatrialAmount int,
@PurchaseAgainst varchar(50),
@Comments varchar(max)
as begin
--Addittion
IF @WorkerLabour = NULL OR ISNULL(@WorkerLabour.'') = ''
BEGIN
	select Id,WithdrawalAmount,Balance,BankName,WorkerLabour,PurchaseMatrialAmount,PurchaseAgainst,Comments from tbldailyreport where IsActive=1 
END
ELSE
BEGIN
--Addittion
if(@ID is null)  
insert into tbldailyreport (WithdrawalAmount,Balance,BankName,WorkerLabour,PurchaseMatrialAmount,PurchaseAgainst,Comments,IsActive,Date ,CreatedBy ,LastUpdatedBy)
values (@WithdrawalAmount,@Balance,@BankName,@WorkerLabour,@PurchaseMatrialAmount,@PurchaseAgainst,@Comments,1,Getdate(),'Admin','Admin')  
else
update tbldailyreport set WithdrawalAmount=@WithdrawalAmount,Balance=@Balance,BankName=@BankName,WorkerLabour=@WorkerLabour,PurchaseMatrialAmount=@PurchaseMatrialAmount,PurchaseAgainst=@PurchaseAgainst,Comments=@Comments,Date = GetDate()
where ID=@ID
end
END--Addittion
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900