Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Stored Procedure

Author  Topic 

rsquared
Starting Member

5 Posts

Posted - 2012-10-18 : 12:00:19
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
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-10-18 : 12:19:16
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 - 2012-10-18 : 14:11:32
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

52326 Posts

Posted - 2012-10-18 : 14:18:13
[code]
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
[/code]

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

Go to Top of Page

rsquared
Starting Member

5 Posts

Posted - 2012-10-18 : 14:32:22
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

52326 Posts

Posted - 2012-10-18 : 14:56:16
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 - 2012-10-18 : 15:10:31
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

52326 Posts

Posted - 2012-10-18 : 15:30:36
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/

Go to Top of Page

rsquared
Starting Member

5 Posts

Posted - 2012-10-18 : 17:55:21
It worked great. Thank for your help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-18 : 21:42:44
welcome

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

Go to Top of Page
   

- Advertisement -