Monday, March 19, 2012
Explicitly replicating one record out of scheduled replication.
Im having a situation where I have configured replication between remote sites and my head office, after every 24 hours. It is working perfectly. Now I want to modify it as per new business requirement. For this I need to know the answers to following questions. Any help will be appreciated.
Consider sample database with a master and child tables.
1) It is possible to replicate master table immediately and child table every 24 hour?
2) is there any possibility that we can explicitly replicate a single child record when required? For example, people can see master record without the need to see child table records, but if some one want to see some specific record from child table, can it be done?
Regards,1. Yes ... use transactional replication for the master table and snapshot replication for the child table with a new snapshot every 24 hours.
2. No ... not without some sort of home-grown solution. Snapshot is like a batch process, transaction (and merge) can be scheduled as immediate or on a time unit basis (1 minute, 10 minutes, etc), but you don't get to mix and match at the row level.
If you build you own solution to this, and use snapshot replication for the child table, be aware that when the snapshot is applied to the subscriber, you will lose any operations that were independently applied to any of the subscriber articles.|||thanks for the reply. but can you please suggest me some solution for my requirement.
let me explain little more.
data entry is being done on remote locations. This data entry is related to big CAD and pdf files. Operators are populating the master table with matadata and child table with the files themselves. so each file has its matadata of less than 1K and the files could be of 90mb at times.
I can replicate master table (matadata table) easily. but if i try to replicate file table, then it slows down the whole process.
i was wondering, if there is any solution if i can control the replication process or something like that. or i can programatically call some procedures to do this type of replication.
regards|||One way I can think of would be to add a replicate flag to the child table and let it default to 0. set the child table up with transactional replication, but alter the filter runle to only replicate the article when the flag = 1. then, when you need to immediately replicate a row, manually update the row to set the flag to 1, let it replicate, then reset it to 0.
You will still have to apply the new snapshot every 24 hours to ensure all rows are current, or you could keep track manually of all rows modified (added, updated, or deleted) during a 24 hour period by PK, then use a job to set the repl flag to 1 for added and updated rows, and manually delete deleted rows.
Just my thoughts on one way to do it ... not easy but workable.
Explicitly fail a SQL Server Job in SQL Server 2000
(step 3) looks for a record in the database. How can I explicitly fail
the SQL server job if the database record does not exist?You can use RAISERROR with a severity level of 11 or higher to indicate
failure.
Simon|||Thanks Simon. That worked.
Monday, March 12, 2012
EXPERT: Stored procedure operand type clash
I have the following sp:
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET
ANSI_NULLSONGO
SET
QUOTED_IDENTIFIERONGO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE
PROCEDURE spNewMessage@.UserIDSender
nvarchar(256),@.MessageTitle
nvarchar(50),@.MessageContent
text,@.MessageType
int,@.MessageID
uniqueidentifier,@.UserID
uniqueidentifierAS
Begin
SetNoCountonDECLARE @.WhateverIDuniqueidentifierINSERT
INTO tblMessages(UserIDSender,MessageTitle,MessageContent,MessageType)VALUES
(@.UserIDSender,@.MessageTitle,@.MessageContent,@.MessageType)Select @.WhateverID=@.@.IdentityINSERT
INTO tblUsersAndMessages(MessageID,UserID)VALUES
(@.WhateverID,@.UserID)End
GO
And here are my tables:
tblUsersAndMessages
allow nulls
MessageID uniqueidentifier false
UserID uniqueidentifier false
New bit false *default set to ((1))
tblMessages
allow nulls
MessageID uniqueidentifier false *PK default set to (newid())
UserIDSender uniqueidentifier false
MessageTitle nvarchar(50) true
MessageContent text true
SentDateTime datetime false * default set to (getdate())
MessageType int false
Now when I try to add my SP I get this error:
Msg 206, Level 16, State 2, Procedure spNewMessage, Line 22
Operand type clash: numeric is incompatible with uniqueidentifier
What can this be?!?
Could you provide me with an example?
Thanks!|||Hi. You can us it as follows:
SET @.WhateverID = SCOPE_IDENTITY()
It may not solve your problem, but it's something you should do anyway. Good luck!|||
That's because SCOPE_IDENTITY is used to the lastIDENTITY value inserted into an IDENTITY column in the same scope, so it will return a numberic value, rather than uniqueidentifier. So we have to retrive the UNIQUEIDENTIFIER of the last inserted row from the table, for example:
ALTER PROCEDURE spNewMessage @.UserIDSenderuniqueidentifier, @.MessageTitle nvarchar(50),
@.MessageContent text, @.MessageType int, @.MessageID uniqueidentifier,
@.UserID uniqueidentifier
AS
Begin
Set NoCount on
DECLARE @.WhateverID uniqueidentifier
INSERT
INTO tblMessages(UserIDSender,MessageTitle,MessageContent,MessageType)
VALUES
(@.UserIDSender,@.MessageTitle,@.MessageContent,@.MessageType)
Select @.WhateverID=MessageID FROM tblMessages ORDER BY SentDateTime ASC
INSERTINTO tblUsersAndMessages(MessageID,UserID)
VALUES(@.WhateverID,@.UserID)
End
GO
Friday, March 9, 2012
Expects parameter which was not supplied....need help....
Not so sure I coded it correctly. And I got this error trying to insert new record
" Procedure 'sp_insert' expects parameter '@.mid', which was not supplied. "
Isn't it i have include @.mid in my code ...??
<STORED PROCEDURES>
sp_insert
(
@.mid char(10),
@.status char(10),
@.name char(20),
@.dj char(20),
@.rank char(20),
@.nric char(14),
@.dob char(20),
@.age char(10),
@.add char(80),
@.school char(50),
@.hp char(10),
@.mp char(10),
@.email char(30),
@.pgname char(20),
@.relationship char(20),
@.contact char(10)
)
AS
insert into Table1(mid,status,name,date_joined,rank,nric,dob,age,address,school,house_phone,Mobile_phone,email,[P/G_name],[P/G_relationship],[P/G_contacts])
values (@.mid,@.status,@.name,@.dj,@.rank,@.nric,@.dob,@.age,@.add,@.school,@.hp,@.mp,@.email,@.pgname,@.relationship,@.contact)
insert into Table2 (name) values (@.name)
RETURN
<WEB FORM>
mycommand = New SqlCommand("sp_insert", mycon)
mycommand.CommandType = CommandType.StoredProcedure
Dim midpar As New SqlParameter("@.mid", SqlDbType.Char, 10)
midpar.Direction = ParameterDirection.Input
midpar.Value = Tb1.Text
Dim statuspar As New SqlParameter("@.status", SqlDbType.Char, 10)
statuspar.Direction = ParameterDirection.Input
statuspar.Value = ddl1.SelectedItem.Text
---
---
---Your webform code is wrong, needs to be something like this:
Dim sqlPar As SqlParameter= myCommand.Parameter.Add("@.mid", SqlDbType.Char, 10)
sqlPar.Direction = ParameterDirection.Input
sqlPar.Value = Tb1.TextsqlPar = myCommand.Parameter.Add("@.status", SqlDbType.Char, 10)
sqlPar.Direction = ParameterDirection.Input
sqlPar.Value = ddl1.SelectedItem.Text
etc.
etc.
etc.
Sunday, February 26, 2012
exists: the absence of data
ThanksYou can use the NOT EXISTS condition in your WHERE clause to find rows in the parent table that have no related rows in the child table.
There are lots of good books on Transact-SQL, as well as SQL in general. If all you need is a good introduction, the SQL for Dummies (http://www.dummies.com/WileyCDA/DummiesTitle/productCd-0764540750.html) will probably do the job.
-PatP|||Thanks Pat. Works exactly how I wanted it. Thanks for the book rec. too.
Adam
Originally posted by Pat Phelan
You can use the NOT EXISTS condition in your WHERE clause to find rows in the parent table that have no related rows in the child table.
There are lots of good books on Transact-SQL, as well as SQL in general. If all you need is a good introduction, the SQL for Dummies (http://www.dummies.com/WileyCDA/DummiesTitle/productCd-0764540750.html) will probably do the job.
-PatP
EXISTS or other construct to achieve FK table record deletion
detail table before deleting the header record from the pk table. I have pu
t
together the following SQL. The SELECT statement gets the 2 records I need
from the detail table, but I'm not sure how to pass the ID's to the DELETE
part of the statement to get id of the details records. I have the overall
statement as a SELECT for now, until I work it all out. I will then change
the SELECT to a DELETE. I have included both versions for your review. I
would certainly appreciate your assistance. I can give you my phone number
if you'd like to speak in person more about my effort.
Gratefully,
Mark
CODE SAMPLE:
--
SELECT *
FROM jrnDTLGrantManagement AS Tdtl
WHERE EXISTS
-->this part gets the 2 records I'm looking for but the above returns 8000 +
record
-->ignoring the criteri below. Am I using the EXISTS incorrectly
-->TSum.IDJ is the PK below
-->Tdtl.IDJ is the FK
(SELECT * FROM jrnGrantManagement AS Tsum
INNER JOIN jrnDTLGrantManagement AS TDtl2 ON TSum.Idj = TDtl2.Idj
INNER JOIN mbrInitiative AS TInit ON Tdtl2.Initiative = TInit.ID
WHERE TInit.ParentH1 = 'CanadianBorder'
AND CONVERT(CHAR(8),TSum.DateModified,10) = '01-11-06')
when the above works, I plan to convert it to a DELETE statement as follows:
DELETE jrnDTLGrantManagement
FROM jrnDTLGrantManagement AS Tdtl
WHERE EXISTS
(SELECT * FROM jrnGrantManagement AS Tsum
INNER JOIN jrnDTLGrantManagement AS TDtl2 ON TSum.Idj = TDtl2.Idj
INNER JOIN mbrInitiative AS TInit ON Tdtl2.Initiative = TInit.ID
WHERE TInit.ParentH1 = 'CanadianBorder'
AND CONVERT(CHAR(8),TSum.DateModified,10) = '01-11-06')You should be able to accomplish this by creating a relationship with
"Cascade Delete Related Fields" selected.
This should let the database handle the deletes in the detail table
automatically when a record from the PK table is deleted. This requires no
coding, just point and click.
That said, if you really want to do it this way, here are a couple of
suggestions...
First, replace the * in your exists select with a litteral 1. I'm not
certain how SQL Server handles this, btu when I worked with Oracle selecting
1 rather than fields was less overhead, as no data had to actually be placed
in memory save the one. If anyone knows if this really makes a difference
or not, I would love to knwo for sure.
It looks like the problem with your exists is that you are not joining it to
your table in your select statement, so it is finding one row exists
regardless of which row you are looking at in the main select. Try adding a
criteria to your exists where it joins to the outer select (delete).
where TDtl2.Idj = TDtl.Idj
You could also make your exists an in clause, and select TDtl2.Idj in place
of *. Then just chang "where exists" to "where TDtl.Idj in" I'm not sure
how this will affect performance as I dont know your table structures.
Hope this helps.
"Mark in Miami" <MarkinMiami@.discussions.microsoft.com> wrote in message
news:BE80A9BA-401A-4E73-B4AC-4093F72A6D8B@.microsoft.com...
> I need one other favor. I am trying to delete the detail records from the
> detail table before deleting the header record from the pk table. I have
put
> together the following SQL. The SELECT statement gets the 2 records I
need
> from the detail table, but I'm not sure how to pass the ID's to the DELETE
> part of the statement to get id of the details records. I have the
overall
> statement as a SELECT for now, until I work it all out. I will then
change
> the SELECT to a DELETE. I have included both versions for your review. I
> would certainly appreciate your assistance. I can give you my phone
number
> if you'd like to speak in person more about my effort.
> Gratefully,
> Mark
> CODE SAMPLE:
> --
> SELECT *
> FROM jrnDTLGrantManagement AS Tdtl
> WHERE EXISTS
> -->this part gets the 2 records I'm looking for but the above returns 8000
+
> record
> -->ignoring the criteri below. Am I using the EXISTS incorrectly
> -->TSum.IDJ is the PK below
> -->Tdtl.IDJ is the FK
> (SELECT * FROM jrnGrantManagement AS Tsum
> INNER JOIN jrnDTLGrantManagement AS TDtl2 ON TSum.Idj = TDtl2.Idj
> INNER JOIN mbrInitiative AS TInit ON Tdtl2.Initiative = TInit.ID
> WHERE TInit.ParentH1 = 'CanadianBorder'
> AND CONVERT(CHAR(8),TSum.DateModified,10) = '01-11-06')
> when the above works, I plan to convert it to a DELETE statement as
follows:
> DELETE jrnDTLGrantManagement
> FROM jrnDTLGrantManagement AS Tdtl
> WHERE EXISTS
> (SELECT * FROM jrnGrantManagement AS Tsum
> INNER JOIN jrnDTLGrantManagement AS TDtl2 ON TSum.Idj = TDtl2.Idj
> INNER JOIN mbrInitiative AS TInit ON Tdtl2.Initiative = TInit.ID
> WHERE TInit.ParentH1 = 'CanadianBorder'
> AND CONVERT(CHAR(8),TSum.DateModified,10) = '01-11-06')
>|||DELETE jrnDTLGrantManagement
FROM jrnDTLGrantManagement AS Tdtl
WHERE EXISTS
(SELECT * FROM jrnGrantManagement AS Tsum
INNER JOIN mbrInitiative AS TInit ON Tdtl2.Initiative = TInit.ID
WHERE TInit.ParentH1 = 'CanadianBorder'
AND CONVERT(CHAR(8),TSum.DateModified,10) = '01-11-06'
)
This makes it a correlated subquery, and should work. First of course,
check my work by executing:
SELECT Tdtl.*
FROM jrnDTLGrantManagement AS Tdtl
WHERE EXISTS
(SELECT * FROM jrnGrantManagement AS Tsum
INNER JOIN jrnDTLGrantManagement AS TDtl2 ON TSum.Idj = TDtl2.Idj
INNER JOIN mbrInitiative AS TInit ON Tdtl2.Initiative = TInit.ID
WHERE TInit.ParentH1 = 'CanadianBorder'
AND CONVERT(CHAR(8),TSum.DateModified,10) = '01-11-06'
AND TSum.Idj = TDtl.Idj
)
And make sure the rows that are returned are the ones you actually want
deleted. (Since you didn't give me a table or data to test out, I may have
made a mistake!)
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"Mark in Miami" <MarkinMiami@.discussions.microsoft.com> wrote in message
news:BE80A9BA-401A-4E73-B4AC-4093F72A6D8B@.microsoft.com...
>I need one other favor. I am trying to delete the detail records from the
> detail table before deleting the header record from the pk table. I have
> put
> together the following SQL. The SELECT statement gets the 2 records I
> need
> from the detail table, but I'm not sure how to pass the ID's to the DELETE
> part of the statement to get id of the details records. I have the
> overall
> statement as a SELECT for now, until I work it all out. I will then
> change
> the SELECT to a DELETE. I have included both versions for your review. I
> would certainly appreciate your assistance. I can give you my phone
> number
> if you'd like to speak in person more about my effort.
> Gratefully,
> Mark
> CODE SAMPLE:
> --
> SELECT *
> FROM jrnDTLGrantManagement AS Tdtl
> WHERE EXISTS
> -->this part gets the 2 records I'm looking for but the above returns 8000
> +
> record
> -->ignoring the criteri below. Am I using the EXISTS incorrectly
> -->TSum.IDJ is the PK below
> -->Tdtl.IDJ is the FK
> (SELECT * FROM jrnGrantManagement AS Tsum
> INNER JOIN jrnDTLGrantManagement AS TDtl2 ON TSum.Idj = TDtl2.Idj
> INNER JOIN mbrInitiative AS TInit ON Tdtl2.Initiative = TInit.ID
> WHERE TInit.ParentH1 = 'CanadianBorder'
> AND CONVERT(CHAR(8),TSum.DateModified,10) = '01-11-06')
> when the above works, I plan to convert it to a DELETE statement as
> follows:
> DELETE jrnDTLGrantManagement
> FROM jrnDTLGrantManagement AS Tdtl
> WHERE EXISTS
> (SELECT * FROM jrnGrantManagement AS Tsum
> INNER JOIN jrnDTLGrantManagement AS TDtl2 ON TSum.Idj = TDtl2.Idj
> INNER JOIN mbrInitiative AS TInit ON Tdtl2.Initiative = TInit.ID
> WHERE TInit.ParentH1 = 'CanadianBorder'
> AND CONVERT(CHAR(8),TSum.DateModified,10) = '01-11-06')
>|||Do what people have told you, then add a REFERENCES.. ON DELETE CASCADE
clause to the referencing table. Also, please learn why rows are not
anythignlike records, and you will not findyourself in this situation
again
--CELKO--
Please post DDL in a human-readable format and not a machine-generated
one. This way people do not have to guess what the keys, constraints,
DRI, datatypes, etc. in your schema are. Sample data is also a good
idea, along with clear specifications.
*** Sent via Developersdex http://www.examnotes.net ***|||I should have mentioned this, but only if it truly makes sense. He might
just be doing some data cleanup, or a one-time thing. I usually don't
suggest using cascade (I assume that the FK was in place) unless you truly
want it to happen with no warning whatsoever (which is not always desired.)
Then you can write this sort of code to execute, as it is basically the same
thing that the FK will do anyhow.
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"--CELKO--" <remove.jcelko212@.earthlink.net> wrote in message
news:uQy6ZWtFGHA.3100@.tk2msftngp13.phx.gbl...
> Do what people have told you, then add a REFERENCES.. ON DELETE CASCADE
> clause to the referencing table. Also, please learn why rows are not
> anythignlike records, and you will not findyourself in this situation
> again
> --CELKO--
> Please post DDL in a human-readable format and not a machine-generated
> one. This way people do not have to guess what the keys, constraints,
> DRI, datatypes, etc. in your schema are. Sample data is also a good
> idea, along with clear specifications.
>
> *** Sent via Developersdex http://www.examnotes.net ***
Friday, February 24, 2012
Exists
overall output ?
eg.
SELECT *
FROM table1
WHERE EXISTS ( SELECT * FROM table2 WHERE table2.field1 = blah)
Will the overall output the same when
1) the subquery returns 1 record
2) the subquery returns 100 recordsFirst of all there are not records in SQL. You missed the entire point
of RDBMS in your database class.
Then you slept thru your logic class. You have no subquery in what you
posted. All you have is an EXISTS() predicate. Predicates are
subqueries!!
You also have no COUNT() aggregate in your example, so how do we know
what it returns'
Would you like to try to ask a question that makes any kind of sense?|||From the Books Online:
<Excerpt href="http://links.10026.com/?link=acdata.chm::/ac_8_qd_11_9u5v.htm">
When a subquery is introduced with the keyword EXISTS, it functions as an
existence test. The WHERE clause of the outer query tests for the existence
of rows returned by the subquery. The subquery does not actually produce any
data; it returns a value of TRUE or FALSE.
</Excerpt>
Consequently, the output is the same as long as at least one row satisfies
the criteria. The EXISTS condition is false when no rows match.
Hope this helps.
Dan Guzman
SQL Server MVP
"Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
news:u8BZ97KDFHA.960@.TK2MSFTNGP09.phx.gbl...
> Does the record count inside subquery in the Exists clause affect the
> overall output ?
> eg.
> SELECT *
> FROM table1
> WHERE EXISTS ( SELECT * FROM table2 WHERE table2.field1 = blah)
>
> Will the overall output the same when
> 1) the subquery returns 1 record
> 2) the subquery returns 100 records
>
>|||
--CELKO-- wrote:
>First of all there are not records in SQL. You missed the entire point
>of RDBMS in your database class.
>
Or he had a teacher who made more than one point in the course.
>Then you slept thru your logic class. You have no subquery in what you
>posted. All you have is an EXISTS() predicate. Predicates are
>subqueries!!
>
Huh? What's the thing inside the exists predicate if it
is not a subquery? From the SQL-92 standard, which
I believe you helped write:
<exists predicate> ::= EXISTS <table subquery>
Then again, I'm not sure how the Celko logic goes here. In
my logic course, these three statements of yours can never
all be true.
1. Your example contains no subquery.
2. Your example contains an EXISTS() predicate.
3. Predicates are subqueries
(All database programmers are poodles, too?)
>You also have no COUNT() aggregate in your example, so how do we know
>what it returns'
>
He asked about the row count of the subquery, not COUNT().
For the record (pun intended), I believe SQL Server handles
an <exists predicate> as if it contained a proprietary and
possibly nondeterministic TOP 1. In other words, The entire
result set of the table subquery after EXISTS is *not*
evaluated just because it's there.
>Would you like to try to ask a question that makes any kind of sense?
>
Will you return the favor if he does?
Steve Kass
Drew University|||
> Huh? What's the thing inside the exists predicate if it
> is not a subquery? From the SQL-92 standard, which
> I believe you helped write:
> <exists predicate> ::= EXISTS <table subquery>
> Then again, I'm not sure how the Celko logic goes here. In
> my logic course, these three statements of yours can never
> all be true.
> 1. Your example contains no subquery.
> 2. Your example contains an EXISTS() predicate.
> 3. Predicates are subqueries
I was wondering about the same thing.
> (All database programmers are poodles, too?)
How did you find out?
Sincerely,
Chris O.|||Actually, I know the EXISTS return FALSE when now records in subquery and
TRUE when there is records returned.
The thing I in the dark is using this EXISTS predicate, how the subquery
relate to the outer query ?
SELECT *
FROM table1
WHERE EXISTS ( SELECT * FROM table2 WHERE table2.field1 = blah)
The subquery has no relationship with the outer table1.
> Consequently, the output is the same as long as at least one row satisfies
> the criteria. The EXISTS condition is false when no rows match.|||I see what you're trying to say. You need to relate the queries yourself in
the WHERE clause of the subquery, something like this:
SELECT *
FROM table1
WHERE EXISTS (SELECT * FROM table2 WHERE table2.field1 = table1.field1)
The correlation you're looking for is between a column in the table1 outer
query and a column in the table2 subquery. You introduce that via the WHERE
clause of the subquery. I can't imagine too many situations where you would
add an EXISTS predicate with a subquery that didn't relate to your outer
query, at least none that couldn't be better modeled using a simple JOIN in
the outer query.
Thanks,
Michael C#
"Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
news:eoE9neMDFHA.464@.TK2MSFTNGP15.phx.gbl...
> Actually, I know the EXISTS return FALSE when now records in subquery and
> TRUE when there is records returned.
> The thing I in the dark is using this EXISTS predicate, how the subquery
> relate to the outer query ?
> SELECT *
> FROM table1
> WHERE EXISTS ( SELECT * FROM table2 WHERE table2.field1 = blah)
> The subquery has no relationship with the outer table1.
>
>|||> The subquery has no relationship with the outer table1.
The EXISTS predicate is still evaluated regardless of whether or not the
subquery is correlated to the outer table. Whether or not this makes
logical sense depends on what you are trying to accomplish.
CREATE TABLE table1
(
blah int NOT NULL PRIMARY KEY
)
CREATE TABLE table2
(
field1 int NOT NULL PRIMARY KEY
)
INSERT INTO table1 values(1)
INSERT INTO table1 values(2)
INSERT INTO table2 values(1)
INSERT INTO table2 values(3)
SELECT *
FROM table1
WHERE EXISTS (SELECT * FROM table2 WHERE table2.field1 = blah)
SELECT *
FROM table1
WHERE EXISTS (SELECT * FROM table2)
Hope this helps.
Dan Guzman
SQL Server MVP
"Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
news:eoE9neMDFHA.464@.TK2MSFTNGP15.phx.gbl...
> Actually, I know the EXISTS return FALSE when now records in subquery and
> TRUE when there is records returned.
> The thing I in the dark is using this EXISTS predicate, how the subquery
> relate to the outer query ?
> SELECT *
> FROM table1
> WHERE EXISTS ( SELECT * FROM table2 WHERE table2.field1 = blah)
> The subquery has no relationship with the outer table1.
>
>|||So now take the example from Dan and Michael:
SELECT *
FROM table1
WHERE EXISTS (SELECT * FROM table2 WHERE table2.field1 = table1.blah)
Is the outer table execute row by row:
For the first row, the value of the table1.blah in first row(ie. field value
is 1) will be tested in the subquery and returns that in EXISTS predicate.
If it is true, then return the first row.
Then the second row, table1.blah (ie. field value is 2) and tested in the
predicate ?
> CREATE TABLE table1
> (
> blah int NOT NULL PRIMARY KEY
> )
> CREATE TABLE table2
> (
> field1 int NOT NULL PRIMARY KEY
> )
> INSERT INTO table1 values(1)
> INSERT INTO table1 values(2)
> INSERT INTO table2 values(1)
> INSERT INTO table2 values(3)
> SELECT *
> FROM table1
> WHERE EXISTS (SELECT * FROM table2 WHERE table2.field1 = blah)
> SELECT *
> FROM table1
> WHERE EXISTS (SELECT * FROM table2)
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Alan" <NOSPAMalan_pltse@.yahoo.com.au> wrote in message
> news:eoE9neMDFHA.464@.TK2MSFTNGP15.phx.gbl...
and
>|||>> Is the outer table execute row by row: For the first row, the value
of the table1.blah in first row(ie. field [sic] value is 1) will be
tested in the subquery and returns that in EXISTS predicate. If it is
true, then return the first row. Then the second row, table1.blah (ie.
field [sic] value is 2) and tested in the predicate ? <<
It could, but probably will not in most products with a good optimizer.
Other ways of doing this:
0) EXISTS (SELECT * FROM table2) should be executed by looking at the
row count in the stats or other schema information table. But even a
correlated subquery expression will get a row count check first to see
if the table is empty or not.
1) See if a PK-FK relationship exists between the tables, so we know
without looking at any rows that there are matches.
2) See if an index exists and search it instead of the base table.
3) Use hashing to build buckets in parallel and which values match
(Teradata)
4) As you get a match, build a working table in main storage on the fly
which you search before doing any joins (DB2, I think).
5) Use bit vectors on the common domain to look for matches (Nucleus).
And the next execution of the same query could change the method used,
based on statistics or a new optimizer or a new access method.
You are still thinking in terms of a sequential file system model of
execution instead of sets. Unless you are designing an optimizer, you
don't really care about the low-level details.