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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update then Insert based off update

Author  Topic 

robc
Yak Posting Veteran

60 Posts

Posted - 2009-02-12 : 16:20:36
I have an table (tblAvail)

listID,userID,day,year,status

This table tracks availability for users. Now sometimes I need to update the day and year based on the listID. When I do that, I must insert a record for each user with that listID which sets thier status to 0 and listID to 0(because the previous record for that day still needs to be kept, but with different values).

i'm not really sure how I should go about this. I'd like to keep it in SQL if possible. Or my other option is to just loop through each record in .net and update it that way.

thanks!

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-02-12 : 16:30:15
Can you post sample data and output please.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-02-12 : 16:31:35
create a SELECT statement that returns all the rows that you want to generate an INSERT for. Then change the column values to the constants (or expressions) that you want to insert. Then INSERT the results of your new statement into the table.

If you need help with specifics then you need to post specifics. Follow the instructions in the link to do so:

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Be One with the Optimizer
TG
Go to Top of Page

robc
Yak Posting Veteran

60 Posts

Posted - 2009-02-12 : 18:01:45
Thanks for the tips. Let me try and give you a better idea.

Here is my table.

CREATE TABLE [dbo].[tblAvailability2](
[userID] [int] NOT NULL,
[year] [int] NOT NULL,
[day] [int] NOT NULL,
[tag] [varchar](250) NULL,
[availStatus] [tinyint] NULL,
[sheetID] [int] NULL,
CONSTRAINT [PK_tblAvailability2] PRIMARY KEY CLUSTERED
(
[userID] ASC,
[year] ASC,
[day] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


Sample Data

INSERT INTO tblAvailability2(sheetID, userID, day,year,availstatus,tag)
SELECT 1, 5, 256,2009,3,null
SELECT 1, 6, 256,2009,3 ,null
SELECT 1, 7, 256,2009,3 ,null
select 1, 8, 256,2009,3,null


Here is my SP.


CREATE PROCEDURE [dbo].[updateActorAvailability] 
-- Add the parameters for the stored procedure here
@listID int,
@oldDay smallint,
@oldYear smallint,
@newWorkDay smallint,
@newWorkYear smallint,
@availStatus tinyint

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

-- change the availability date for that sheet
UPDATE tblAvailability2
SET availStatus = @availStatus,
[day] = @newDay,[year] = @newYear
WHERE sheetID = @listID

-- re-insert previous records for all users of that listID for oldDay and oldYear, and set thier status to 0

END


i hope this helps.
Go to Top of Page
   

- Advertisement -