Showing posts with label t-sql. Show all posts
Showing posts with label t-sql. Show all posts

Friday, March 23, 2012

Produce data -> Store in table?

Hi!

I'm quite new to T-SQL and is about to build a small reporting db using SQL.
Most of the data I can move with normal INSERT INTO ... SELECT, but there are some tables that I want to
produce using T-SQL. For example I want to build a Date table like..

Date
Year
Quarter
Month
WeekDay
...

With some precalculated values for each date.

I've searched the forum but have not found any information on how to produce table contents in a good manner. I would appreciate if someone would have the time to point me in the right direction. I do not want to do this by code in my application.

My first thought is to use some kind of Insert Cursor in a While loop...

Pseudo:

declare cursor ex for 'Insert table ... '

while
begin

(produce data)
insert data

end

close cursor

While browsing the net I've got the feeling that you use cursor less in SQL Server than in other db-engines...

Have a nice day!

You could use WHILE loop without cursor:

Code Snippet

create table dates

(

Year int,

Quarter int,

Month int,

WeekDay int,

SomeData decimal

)

go

declare @.start_date datetime

declare @.end_date datetime

set @.start_date = '2007-01-01'

set @.end_date = getdate()

while @.start_date<@.end_date

begin

set @.start_date = dateadd(day, 1, @.start_date)

insert into dates values(

datepart(year,@.start_date),

datepart(quarter,@.start_date),

datepart(month,@.start_date),

datepart(weekday,@.start_date),

rand() --Just for demo

)

end

go

select * from Dates

|||

Best have a calendar table (you can add all the nessasary columns here), fill the table with required date range, join with your table & use the required calculations,

Code Snippet

create table calendar

(

year int,

quarter int,

month int,

weekday int,

date datetime primary key

)

Go

create proc fill_calendar(@.start_date datetime,@.end_date datetime)

as

begin

set nocount on;

while @.start_date <= @.end_date

begin

insert into calendar

select

datepart(year,@.start_date),

datepart(quarter,@.start_date),

datepart(month,@.start_date),

datepart(weekday,@.start_date),

@.start_date

where

not exists(select 1 from calendar where date=@.start_date);

set @.start_date = dateadd(day, 1, @.start_date)

end

end

go

--Fill your calendar when required

exec fill_calendar '1/1/2000','12/31/2007'

select * from calendar

go

select

year --other columns

,sum(yourtable.calculation)

from

calendar

inner join yourtable on yourtable.datefield = calendar.date

group by

year --other columns

Saturday, February 25, 2012

Process Detail (T-SQL)

Hello there :-)
Just want know where can we find the last T-SQL being executed on a process. This is the one that pops-up when we double-clik a process on the Process Info List. Thank You in Advance :-)dbcc inputbuffer (@.spid)

Process Blocking Problem

We are running SQL Server 7.0 Service Pack #4, running on Windows 2000 Server.
We have a problem with process blocking. At some points the T-SQL statement runs restricting all our users from accessing the DB.
The statement is:

SET FMTONLY ON
exec sp_execute 1
SET FMTONLY OFF

Only when it finishes running, then users are allowed to access the db again.
It is triggered automatically. Does any know what condition on the server is causing this statements to run and what can be done to prevent it. Thanks so much for your help.Look at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql7/html/sqlquerproc.asp

It explains what sp_execute does.
Do you have a lot of dynamic sql or embedded sql rather than SPs?|||Also look at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlmag2k/html/adoapp.asp|||What I found out from the Microsoft website that sp_execute is a command that runs a prepared statement using the sp_prepare command.
So what happened is I've watched the SQL for the condition to repeat but it did not. However what I've noticed that our of nowhere at time sp_prepare runs without any parameters, causing the CPU utilization go up to high 90%.
What is causing this sp_prepate to run, as it done automatically.
Thanks for your help.

process all row

how would i do this in pure t-sql not in a win app
i have column A B and C all integers
psuedo sql
INSERT INTO TABLE1 (C) values (A+B)
A+B = C
thanksHoward
CREATE TABLE #Test
(
A INT,
B INT,
C AS COALESCE(A,0)+COALESCE(B,0)
)
INSERT INTO #Test SELECT 1,2
SELECT * FROM #Test
"Howard" <howdy0909@.yahoo.com> wrote in message
news:u3lkECkUGHA.776@.TK2MSFTNGP09.phx.gbl...
> how would i do this in pure t-sql not in a win app
> i have column A B and C all integers
> psuedo sql
> INSERT INTO TABLE1 (C) values (A+B)
> A+B = C
> thanks
>|||Exactly as you stated
Jack Vamvas
___________________________________
Receive free SQL tips - www.ciquery.com/sqlserver.htm
"Howard" <howdy0909@.yahoo.com> wrote in message
news:u3lkECkUGHA.776@.TK2MSFTNGP09.phx.gbl...
> how would i do this in pure t-sql not in a win app
> i have column A B and C all integers
> psuedo sql
> INSERT INTO TABLE1 (C) values (A+B)
> A+B = C
> thanks
>|||oops sorry i meant update
UPDATE table1
SET colC = colA + colB
WHERE loop all
"Jack Vamvas" <delete_this_bit_jack@.ciquery.com_delete> wrote in message
news:LfWdncfWt9dmaLXZRVnyvQ@.bt.com...
> Exactly as you stated
> --
> Jack Vamvas
> ___________________________________
> Receive free SQL tips - www.ciquery.com/sqlserver.htm
>
> "Howard" <howdy0909@.yahoo.com> wrote in message
> news:u3lkECkUGHA.776@.TK2MSFTNGP09.phx.gbl...
>|||An unrestricted update (no WHERE clause) will process the entire table.
E.g.:
UPDATE table1
SET colC = colA + colB
ML
http://milambda.blogspot.com/|||Also you dont need to have other column. Just use select
Select colA, colB, colA+colB as colC from table
Madhivanan