SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rsquared
Starting Member

5 Posts

Posted - 10/18/2012 :  12:00:19  Show Profile  Reply with Quote
I need to write a stored procedure that will change or update a boolean field to true if it is false, and false if it is true. In order to update the correct record, I need to use the record number as a parameter.Thanks !

Lamprey
Flowing Fount of Yak Knowledge

4363 Posts

Posted - 10/18/2012 :  12:19:16  Show Profile  Reply with Quote
What have you tried so far? Can you post your DDL & DML (expected output would be nice too). Here are some links that can help you prepare that:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

rsquared
Starting Member

5 Posts

Posted - 10/18/2012 :  14:11:32  Show Profile  Reply with Quote
quote:
Originally posted by Lamprey

What have you tried so far? Can you post your DDL & DML (expected output would be nice too). Here are some links that can help you prepare that:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx





ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON

-- Insert statements for procedure here


SELECT CASE


WHEN Active = 1
UPDATE
Set Active= 0
ELSE
UPDATE
Set Active = 1

END , LastName,Active
FROM Demographics
WHERE MR=@MR
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 10/18/2012 :  14:18:13  Show Profile  Reply with Quote

ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON

-- Insert statements for procedure here


SELECT ~Active , LastName,Active
FROM Demographics 
WHERE MR=@MR


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rsquared
Starting Member

5 Posts

Posted - 10/18/2012 :  14:32:22  Show Profile  Reply with Quote
quote:
Originally posted by visakh16


ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON

-- Insert statements for procedure here


Thanks but where is the part that will determine if Active is true? And if it is true I want to update it to false????


SELECT ~Active , LastName,Active
FROM Demographics 
WHERE MR=@MR


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 10/18/2012 :  14:56:16  Show Profile  Reply with Quote
quote:
Originally posted by rsquared

quote:
Originally posted by visakh16


ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON

-- Insert statements for procedure here


Thanks but where is the part that will determine if Active is true? And if it is true I want to update it to false????


SELECT ~Active , LastName,Active
FROM Demographics 
WHERE MR=@MR


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/






~Active will cause 1 to become 0 and 0 to become 1 . no need of any checking conditions. ~ is unary not operator

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rsquared
Starting Member

5 Posts

Posted - 10/18/2012 :  15:10:31  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

quote:
Originally posted by rsquared

quote:
Originally posted by visakh16


ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON

-- Insert statements for procedure here


Thanks but where is the part that will determine if Active is true? And if it is true I want to update it to false????


SELECT ~Active , LastName,Active
FROM Demographics 
WHERE MR=@MR


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/






~Active will cause 1 to become 0 and 0 to become 1 . no need of any checking conditions. ~ is unary not operator

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





I don't get it. It is not changing the values form 0 to 1????
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 10/18/2012 :  15:30:36  Show Profile  Reply with Quote
are you asking about changing values in tables or just changing why displaying data?

If former then it should be an update like


ALTER PROCEDURE [dbo].[spCheckActivePaient]
-- Add the parameters for the stored procedure here
@MR varchar(50)
AS

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON

-- Insert statements for procedure here


UPDATE d
SET Active = ~Active 
FROM Demographics d
WHERE MR=@MR





If latter then my posted suggestion works



here's an illustration for you as well



declare @test table
(
i bit,
r int
)
insert @test
values (1,123)

insert @test
values (0,110)

insert @test
values (1,234)

select i,~i
from @test
where r=123

output
-------------------
i	Negatedi
-------------------
1	0



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 10/18/2012 15:31:17
Go to Top of Page

rsquared
Starting Member

5 Posts

Posted - 10/18/2012 :  17:55:21  Show Profile  Reply with Quote
It worked great. Thank for your help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 10/18/2012 :  21:42:44  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.1 seconds. Powered By: Snitz Forums 2000