Showing posts with label primary. Show all posts
Showing posts with label primary. Show all posts

Wednesday, March 21, 2012

Help with a special stored procedure

Hi

I have a table there have a primary key, this value have i also in another
table as a forreignkey, so good so far.

Here it is
If the number in may primarykey in table 1 , dosn't exits in table 2 then
delete records in table 1

I have made this in a ASP page with a view there list all records where the forreignkey in table 2 are NULL and then delte all records in table one

Can i made this as a stored procedure ?

regards

alvin

You really want a trigger on table 1 to check table 2 before insert.

Research INSTEAD OF Insert Triggers.

What this will do is instead of inserting a new record into table 1, it will check table 2 for a match. If there is no match, no record is inserted into table 1.

It's better to catch the record before it goes in, than to insert the record and then have to remove it later.

Adamus

|||

Hi

I don't believe you understand

When i made a post in table one i also make a post in table 2

All works fine

But tabel 2 i also connected with table 3 and here i have in my diagram a cascade delete

So when i delete a record in table 3 it's delete a record or more in table 2
then i have my record in table 1, this i can't delete when i delete the record in table 3
so what i want is to delete all record in table 1 if the record in table 2 are deleted

hop you understand ?

Alvin

|||

Ok table 3 is new...but the answer is still the same...triggers not sp's

When you delete from any table, also delete from other tables...correct?

So you want INSTEAD OF DELETE

Adamus

|||

I try to explain and maybe you can help

In table 1 i have a ID theis ID can bee many times in table 2
Table 2 have also a int there connect to table3

all works fine

When a date field in table3 is over current date then i delete the record in table
And when it does this it also delete the record in table2.

after sometime all the records in table2 there have the ID from table 1 is been deleted
and when there are no more record in table2 there in my feild have the same numbers
as the ID in table 1 then i wnt to delete the record in table 1

Like:

Delete all records in table 1 if table1.ID <> from table2.field

In vbscript i can make a loop to check if the Id from table1 are in table2
if the catch this number = do nothing
if the find the number = delte record

i Can't explain it better. Sorry

Alvin

|||

It looks to me like you're wanting something like this:

create procedure delete_records_from_table1

as

delete from table1

from table1

left join table2

on table1.ID = table2.ID

where table2.ID is null

The left join will include everything in table1, regardless of whether there's a matching record in table2, and then the where clause excludes all of the records where there is a match in table2. This just leaves those records where there's no match in table2, and these can be deleted.

Is this the sort of thing you were looking for?

Iain

|||

Yes yes yes

Thanks

Alvin

sql

Wednesday, March 7, 2012

Help to optimize query

Hi,
I have these two tables in a Database

ITEMS
IDnumeric (Primary key)
ZDIDnvarchar 3 (not null)
IDF_Familynumeric(not null)
Descriptionnvarchar40 (not null)

DATAS
IDnumeric(Primary Key)
IDF_Itemnumeric(Foreign key)
IDF_Referencenumeric(Foreign Key)
[Date]smalldatetime(not null)
Containernchar10(not null)
Averagedecimal(not null)
[%Compliance]decimal(not null)
[%OutOfRange<MinTg]decimal(not null)
[%OutOfRange>MaxTg]decimal(not null)
Targetdecimal(not null)
[Min]decimal(not null)
[Max]decimal(not null)

The table DATAS has 4000000+ records

I'm running this query:

SELECT DISTINCT I.ID, I.ZDID, I.IDF_Family, I.Description
FROM Items as I, Datas as D
WHERE D.IDF_Item = I.ID AND I.IDF_Family = 84
AND D.Date BETWEEN '5/18/2004' AND '5/18/2004'

it's taking 4-5 minutes to run.
The result is correct, there is no thing on that date.
I've done a reindex, but still the same thing.

What can I do?

Thanksbie2 (Francois.Tardif@.gmail.com) writes:
> I have these two tables in a Database
> ITEMS
> ID numeric (Primary key)
> ZDID nvarchar 3 (not null)
> IDF_Family numeric (not null)
> Description nvarchar 40 (not null)
> DATAS
> ID numeric (Primary Key)
> IDF_Item numeric (Foreign key)
> IDF_Reference numeric (Foreign Key)
> [Date] smalldatetime (not null)
> Container nchar 10 (not null)
> Average decimal (not null)
> [%Compliance] decimal (not null)
> [%OutOfRange<MinTg] decimal (not null)
> [%OutOfRange>MaxTg] decimal (not null)
> Target decimal (not null)
> [Min] decimal (not null)
> [Max] decimal (not null)
>
> The table DATAS has 4000000+ records
> I'm running this query:
> SELECT DISTINCT I.ID, I.ZDID, I.IDF_Family, I.Description
> FROM Items as I, Datas as D
> WHERE D.IDF_Item = I.ID AND I.IDF_Family = 84
> AND D.Date BETWEEN '5/18/2004' AND '5/18/2004'
> it's taking 4-5 minutes to run.
> The result is correct, there is no thing on that date.
> I've done a reindex, but still the same thing.

A shot in the dark: change 84 to convert(numeric, 84). If that does not
cut it, please answer the questions below:

How many rows are there in Items?

Exactly what indexes are there on the table? Please indicate which
indexes that are clustered.

Can you run the query preceeded by SET STATISTICS PROFILE ON, and
post the output?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||>A shot in the dark: change 84 to convert(numeric, 84). If that does not
>cut it, please answer the questions below:
Nothing changed.

>How many rows are there in Items?
30

>Exactly what indexes are there on the table? Please indicate which
>indexes that are clustered.
On DATAS ID is a clustered Index

>Can you run the query preceeded by SET STATISTICS PROFILE ON, and
>post the output?
01SELECT DISTINCT I.ID, I.ZDID, I.IDF_Family, I.Description FROM
Items as I, Datas as D WHERE D.IDF_Item = I.ID AND I.IDF_Family =
convert(numeric, 84) AND D.Date BETWEEN '5/18/2006' AND
'5/18/2006'210NULLNULLNULLNULL18.225489NULLNULLNULL63.874825NULLNULLSELECT0NULL
01 |--Nested Loops(Inner Join, OUTER
REFERENCES:([D].[IDF_Item]))231Nested LoopsInner JoinOUTER
REFERENCES:([D].[IDF_Item])NULL18.2262020.07.6185526E-511063.874825[I].[Description],
[I].[IDF_Family], [I].[ZDID], [I].[ID]NULLPLAN_ROW01.0
01 |--Sort(DISTINCT ORDER BY:([D].[IDF_Item]
ASC))243SortDistinct SortDISTINCT ORDER BY:([D].[IDF_Item]
ASC)NULL18.2262021.1261261E-24.4788996E-41663.866585[D].[IDF_Item]NULLPLAN_ROW01.0
01 | |--Clustered Index
Scan(OBJECT:([AccessReporting].[dbo].[Datas].[PK_Data] AS [D]),
WHERE:([D].[Date]='May 18 2006 12:00AM'))254Clustered Index
ScanClustered Index
ScanOBJECT:([AccessReporting].[dbo].[Datas].[PK_Data] AS [D]),
WHERE:([D].[Date]='May 18 2006 12:00AM')[D].[Date],
[D].[IDF_Item]41.49002556.7212834.96644977861.687733[D].[Date],
[D].[IDF_Item]NULLPLAN_ROW01.0
00 |--Clustered Index
Seek(OBJECT:([AccessReporting].[dbo].[Items].[PK_Items] AS [I]),
SEEK:([I].[ID]=[D].[IDF_Item]), WHERE:([I].[IDF_Family]=84) ORDERED
FORWARD)263Clustered Index SeekClustered Index
SeekOBJECT:([AccessReporting].[dbo].[Items].[PK_Items] AS [I]),
SEEK:([I].[ID]=[D].[IDF_Item]), WHERE:([I].[IDF_Family]=84) ORDERED
FORWARD[I].[Description], [I].[IDF_Family], [I].[ZDID],
[I].[ID]1.06.3284999E-37.9603E-51018.1532737E-3[I].[Description],
[I].[IDF_Family], [I].[ZDID], [I].[ID]NULLPLAN_ROW018.226202|||Tried this simple query

SELECT D.ID
FROM Datas as D
WHERE D.Date BETWEEN '5/18/2006' AND '5/18/2006'

And still took me 3 minutes, So maybe the problem is with the index on
DATAS|||Solved Created a non clustered index for Date and IDF_Items.

Friday, February 24, 2012

Help Running out of disk space.. filegroup question

Thanks so much Paul.
I'll try adding the second file to the primary filegroup.
Just one question about the proportional fill manner
(relative to the %occupied space. If drive E:/ has 471MBs
free and Drive F:\ (which will hold the new file) has 20
GBs free, will Drive E: still fill up and stop the
database or will the data just flow to the F:/ drive?
of the 2 files). Thanks.
Don

>--Original Message--
>Don,
>yes, you can add the new file to the existing filegroup.
>You won't be able to move objects to this file unless you
>create a new filegroup and add the new file to it,
however
>in your case this is probably not required as when new
>space is required in the database, it is added in
>proportional fill manner (relative to the %occupied space
>of the 2 files).
>HTH,
>Paul Ibison
>.
>
IIRC, data will flow to the new empty file before the old file will expand
beyond its current size. Once they both have the same free space
(relative), I'm not sure how SQL Server will decide which one to grow. To
prolong the wait, just make sure the new file is bigger than the old one.
;-)
http://www.aspfaq.com/
(Reverse address to reply.)
<anonymous@.discussions.microsoft.com> wrote in message
news:1ac8701c44f03$2b1996d0$a101280a@.phx.gbl...[vbcol=seagreen]
> Thanks so much Paul.
> I'll try adding the second file to the primary filegroup.
> Just one question about the proportional fill manner
> (relative to the %occupied space. If drive E:/ has 471MBs
> free and Drive F:\ (which will hold the new file) has 20
> GBs free, will Drive E: still fill up and stop the
> database or will the data just flow to the F:/ drive?
> of the 2 files). Thanks.
> Don
> however
|||Thanks Paul and Aaron,
I added the second primary group file to the F:/ Drive and
it looks like the data is going to there much faster than
to the E:/ drive that was filling up. It may be too early
to tell the exact ratio of E to F of data flow. It looks
like this fix saved the day.
Don
>--Original Message--
>IIRC, data will flow to the new empty file before the old
file will expand
>beyond its current size. Once they both have the same
free space
>(relative), I'm not sure how SQL Server will decide which
one to grow. To
>prolong the wait, just make sure the new file is bigger
than the old one.[vbcol=seagreen]
>;-)
>--
>http://www.aspfaq.com/
>(Reverse address to reply.)
>
>
><anonymous@.discussions.microsoft.com> wrote in message
>news:1ac8701c44f03$2b1996d0$a101280a@.phx.gbl...
filegroup.[vbcol=seagreen]
471MBs[vbcol=seagreen]
filegroup.[vbcol=seagreen]
you[vbcol=seagreen]
space
>
>.
>
|||To prevent any further SQL initiated filegrowth on your E: drive you can disable automatic filegrow on the primary datafile
for this particular database all together and just allow the newly created data file on your other partition to grow, and/or add other data files as need arises. This will prevent SQL from filling up the precious remaining disk space (maybe you are alread
y using more than 90%) on your E: drive before the system halts running out of diskspace. I occasionally have to do this to balance disk load on terabyte size dbs.
|||Hi there,
One of our drives is completely filled up with a database file and so I would like to move some of the db objects to a newly created data file on another drive. Can anyone give me a hint on how to do this?
Thanks
Andreas
"Sassan Karai" wrote:

> To prevent any further SQL initiated filegrowth on your E: drive you can disable automatic filegrow on the primary datafile
> for this particular database all together and just allow the newly created data file on your other partition to grow, and/or add other data files as need arises. This will prevent SQL from filling up the precious remaining disk space (maybe you are alre
ady using more than 90%) on your E: drive before the system halts running out of diskspace. I occasionally have to do this to balance disk load on terabyte size dbs.
>
|||Hi
http://support.microsoft.com/directo...Q224071--
Moving SQL Server Databases to a New Location
"Andreas" <Andreas@.discussions.microsoft.com> wrote in message
news:58B7A052-6AAB-4E31-AD6F-A13F29A895CF@.microsoft.com...
> Hi there,
> One of our drives is completely filled up with a database file and so I
would like to move some of the db objects to a newly created data file on
another drive. Can anyone give me a hint on how to do this?[vbcol=seagreen]
> Thanks
> Andreas
>
> "Sassan Karai" wrote:
disable automatic filegrow on the primary datafile[vbcol=seagreen]
created data file on your other partition to grow, and/or add other data
files as need arises. This will prevent SQL from filling up the precious
remaining disk space (maybe you are already using more than 90%) on your E:
drive before the system halts running out of diskspace. I occasionally have
to do this to balance disk load on terabyte size dbs.[vbcol=seagreen]

Help Running out of disk space.. filegroup question

Thanks so much Paul.
I'll try adding the second file to the primary filegroup.
Just one question about the proportional fill manner
(relative to the %occupied space. If drive E:/ has 471MBs
free and Drive F:\ (which will hold the new file) has 20
GBs free, will Drive E: still fill up and stop the
database or will the data just flow to the F:/ drive?
of the 2 files). Thanks.
Don

>--Original Message--
>Don,
>yes, you can add the new file to the existing filegroup.
>You won't be able to move objects to this file unless you
>create a new filegroup and add the new file to it,
however
>in your case this is probably not required as when new
>space is required in the database, it is added in
>proportional fill manner (relative to the %occupied space
>of the 2 files).
>HTH,
>Paul Ibison
>.
>IIRC, data will flow to the new empty file before the old file will expand
beyond its current size. Once they both have the same free space
(relative), I'm not sure how SQL Server will decide which one to grow. To
prolong the wait, just make sure the new file is bigger than the old one.
;-)
http://www.aspfaq.com/
(Reverse address to reply.)
<anonymous@.discussions.microsoft.com> wrote in message
news:1ac8701c44f03$2b1996d0$a101280a@.phx
.gbl...[vbcol=seagreen]
> Thanks so much Paul.
> I'll try adding the second file to the primary filegroup.
> Just one question about the proportional fill manner
> (relative to the %occupied space. If drive E:/ has 471MBs
> free and Drive F:\ (which will hold the new file) has 20
> GBs free, will Drive E: still fill up and stop the
> database or will the data just flow to the F:/ drive?
> of the 2 files). Thanks.
> Don
>
> however|||Thanks Paul and Aaron,
I added the second primary group file to the F:/ Drive and
it looks like the data is going to there much faster than
to the E:/ drive that was filling up. It may be too early
to tell the exact ratio of E to F of data flow. It looks
like this fix saved the day.
Don
>--Original Message--
>IIRC, data will flow to the new empty file before the old
file will expand
>beyond its current size. Once they both have the same
free space
>(relative), I'm not sure how SQL Server will decide which
one to grow. To
>prolong the wait, just make sure the new file is bigger
than the old one.
>;-)
>--
>http://www.aspfaq.com/
>(Reverse address to reply.)
>
>
><anonymous@.discussions.microsoft.com> wrote in message
> news:1ac8701c44f03$2b1996d0$a101280a@.phx
.gbl...
filegroup.[vbcol=seagreen]
471MBs[vbcol=seagreen]
filegroup.[vbcol=seagreen]
you[vbcol=seagreen]
space[vbcol=seagreen]
>
>.
>|||To prevent any further SQL initiated filegrowth on your E: drive you can dis
able automatic filegrow on the primary datafile
for this particular database all together and just allow the newly created d
ata file on your other partition to grow, and/or add other data files as nee
d arises. This will prevent SQL from filling up the precious remaining disk
space (maybe you are alread
y using more than 90%) on your E: drive before the system halts running out
of diskspace. I occasionally have to do this to balance disk load on terabyt
e size dbs.|||Hi there,
One of our drives is completely filled up with a database file and so I woul
d like to move some of the db objects to a newly created data file on anothe
r drive. Can anyone give me a hint on how to do this?
Thanks
Andreas
"Sassan Karai" wrote:

> To prevent any further SQL initiated filegrowth on your E: drive you can d
isable automatic filegrow on the primary datafile
> for this particular database all together and just allow the newly created data fi
le on your other partition to grow, and/or add other data files as need arises. This
will prevent SQL from filling up the precious remaining disk space (maybe you are a
lre
ady using more than 90%) on your E: drive before the system halts running out of diskspace.
I occasionally have to do this to balance disk load on terabyte size dbs.
>|||Hi
[url]http://support.microsoft.com/directory/article.asp?ID=kb;en-us;Q224071--[/url
]
Moving SQL Server Databases to a New Location
"Andreas" <Andreas@.discussions.microsoft.com> wrote in message
news:58B7A052-6AAB-4E31-AD6F-A13F29A895CF@.microsoft.com...
> Hi there,
> One of our drives is completely filled up with a database file and so I
would like to move some of the db objects to a newly created data file on
another drive. Can anyone give me a hint on how to do this?[vbcol=seagreen]
> Thanks
> Andreas
>
> "Sassan Karai" wrote:
>
disable automatic filegrow on the primary datafile[vbcol=seagreen]
created data file on your other partition to grow, and/or add other data
files as need arises. This will prevent SQL from filling up the precious
remaining disk space (maybe you are already using more than 90%) on your E:
drive before the system halts running out of diskspace. I occasionally have
to do this to balance disk load on terabyte size dbs.[vbcol=seagreen]