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
 Insert values in a new table with clauses

Author  Topic 

VeselaApo
Posting Yak Master

114 Posts

Posted - 2010-12-01 : 13:58:10
Hi,

I create a new table and declare its 2 columns and variables. The first columns of the table will be populated with dates. The second column is with money.
Next, I would like to insert values in the table and I use the INSERT INTO statement. I insert the dates in the first column. In the second column I would like to insert one value on certain dates and not in the entire column. How and where do I specify on which dates I want to insert the value? The desired result should look like that:

Date Fee
01-01-2011 , 400
01-02-2011, 0
01-03-2011, 0
01-04-2011, 0
01-05-2011, 400

Currently my code looks like that:


DECLARE @gencalendar TABLE (cal_date DATETIME PRIMARY KEY, Fees float)
DECLARE @beginning_date SMALLDATETIME
DECLARE @end_date SMALLDATETIME
DECLARE @Fee int


SET @beginning_date = '01-01-2011'
set @end_date = @beginning_date
WHILE @end_date <= '01-01-2013'

Begin
INSERT INTO @gencalendar(cal_date, Fees )
VALUES(@end_date, @fees)
SET @end_date = dateadd(DD, 1, @end_date)
set @fee = 400

End

select *
into #All_Dates
from @gencalendar

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-12-01 : 14:00:29
how do you determine which all dates you want to insert value?

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

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-01 : 14:01:24
You could do it with INSERT INTO/SELECT...WHERE or add an IF statement before the INSERT.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

VeselaApo
Posting Yak Master

114 Posts

Posted - 2010-12-01 : 14:10:53
At this stage I would like to insert the FEE only on the beginning date of the calendar. If I do the if statement, it will also be valid for the insert dates part so I dont think this will work.. I am not sure how to use the select statement - can you please elaborate on that? Should it be after set @fee = ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-12-01 : 14:16:19
you can simply do like

INSERT INTO YourTable
SELECT Date,
CASE WHEN Date = @beginning_date THEN @fee ELSE 0 END
FROM dbo.CalendarTable(@beginning_date,@end_date,0)

see the function definition here

http://visakhm.blogspot.com/2010/02/generating-calendar-table.html

also set correct values for beginning end date variables and fee variable


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

Go to Top of Page

VeselaApo
Posting Yak Master

114 Posts

Posted - 2010-12-01 : 14:22:26
I dont have any CalendarTable database - thats why I am not sure that the select statement will work...any other ideas? Thanks for your input!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-01 : 14:25:35
I think you missed visakh's point. His solution is to create the CalendarTable and then use it to achieve what you want.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

VeselaApo
Posting Yak Master

114 Posts

Posted - 2010-12-01 : 14:34:24
So what you both are saying is that the only way I can insert values on specific dates in my table is through select statement from another table/database? I am trying to simplify my code as much as possible - is there another way?...Thanks much for your input!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-01 : 14:55:16
Yes there's other ways, but visakh's way is super easy! If you want a more complex way, then don't use his solution.

The CalendarTable only needs to be created once. Leave it there in case you ever need it again.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

VeselaApo
Posting Yak Master

114 Posts

Posted - 2010-12-01 : 15:16:53
well the problem is that I am creating my table within a procedure and i define the dates in that procedure. It is not possible to use an external calendar at this stage..
Go to Top of Page

TimSman
Posting Yak Master

127 Posts

Posted - 2010-12-01 : 15:21:25
What table are you creating?
Go to Top of Page

VeselaApo
Posting Yak Master

114 Posts

Posted - 2010-12-01 : 15:50:46
I am declaring a table in the beginning of my procedure. Then I insert values in that table. The first column of the table is dates so I specify begin and end date in the procedure parameters.
Go to Top of Page

TimSman
Posting Yak Master

127 Posts

Posted - 2010-12-01 : 15:58:12
I don't think I understand what you want to do, so bear with me.
You want to create a table with a begin date and an end date, and a fee value for each of those. The begin and end dates and the fee value are all provided externally.

Then you want to update another table with the fee where the other table's date matches the begin date. Is it just begin date, end date, or both? Or is it another date altogether?
Go to Top of Page

VeselaApo
Posting Yak Master

114 Posts

Posted - 2010-12-01 : 16:17:53
"You want to create a table with a begin date and an end date, and a fee value for each of those. The begin and end dates and the fee value are all provided externally"

correct up top here.

Then I populate the table with all the dates from begin date to end date in the first column (see code above). What I want to do next is to update this same table with the fee value but only on certain dates (such as the begin date) not throughout the whole column 2. How do I do that?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-01 : 16:55:53
I don't understand why your procedure can't reference the Calendar table. Please explain. Perhaps you need to test out visakh's solution as it solves your problem.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

TimSman
Posting Yak Master

127 Posts

Posted - 2010-12-01 : 17:06:00
Well, how do you know what those "certain dates" are? Is there another table elsewhere? How are you populating the other dates?
Go to Top of Page
   

- Advertisement -