all groups > sql server programming > june 2005 >
You're in the

sql server programming

group:

Error While Updating a Temp Table - Cannot resolve collation confl


Error While Updating a Temp Table - Cannot resolve collation confl Sevugan
6/26/2005 10:55:01 PM
sql server programming:
Hi,
I am passing the name of a temporary table(#TempTable1) to a stored
procedure. I am creating another temporary table inside this stored procedure
(#TempTable2).
I am executing an update statement by joining these 2 temporary tables in
this stored procedure.

UPDATE A
SET A.Field1= ISNULL(B.Field1, 0)
FROM #TempTable1 A, #TempTable2 B
WHERE A.Field2 = B.Field2

At that point of time, I am getting the following error.
"Cannot resolve collation conflict for equal to operation."

Why does this happen. How can I resolve the same?

Thanks and Regards,

Re: Error While Updating a Temp Table - Cannot resolve collation confl Uri Dimant
6/27/2005 12:00:00 AM
Hi

UPDATE A
SET A.Field1= ISNULL(B.Field1, 0)
FROM #TempTable1 A, #TempTable2 B COLLATE .(Put COLLATION...Name here).....
WHERE A.Field2 = B.Field2

Note: It is considered a bad practice to pass a table name as a parameter to
stored procedure as well as using such tecnique for updating

Please take a look at David Portas's example how this tecnique can affect
the output.
CREATE TABLE Countries
(countryname VARCHAR(20) NOT NULL PRIMARY KEY,
capitalcity VARCHAR(20));

CREATE TABLE Cities
(cityname VARCHAR(20) NOT NULL,
countryname VARCHAR(20) NOT NULL
REFERENCES Countries (countryname),
CONSTRAINT PK_Cities
PRIMARY KEY (cityname, countryname));

INSERT INTO Countries (countryname, capitalcity) VALUES ('USA', NULL);
INSERT INTO Countries (countryname, capitalcity) VALUES ('UK', NULL);

INSERT INTO Cities VALUES ('Washington', 'USA');
INSERT INTO Cities VALUES ('London', 'UK');
INSERT INTO Cities VALUES ('Manchester', 'UK');

The MS-syntax makes it all too easy for the developer to slip-up by
writing ambiguous UPDATE...FROM statements where the JOIN criteria is
not unique on the right side of the join.

Try these two identical UPDATE statements with a small change to the
primary key in between.

UPDATE Countries
SET capitalcity = cityname
FROM Countries JOIN Cities /* evil UPDATE... FROM syntax */
ON Countries.countryname = Cities.countryname;

SELECT * FROM Countries;

ALTER TABLE Cities DROP CONSTRAINT PK_Cities;
ALTER TABLE Cities ADD CONSTRAINT PK_Cities PRIMARY KEY (countryname,
cityname);

UPDATE Countries
SET capitalcity = cityname
FROM Countries JOIN Cities /* don't do this! */
ON Countries.countryname = Cities.countryname;

SELECT * FROM Countries;

You get this from the first SELECT statement:

countryname capitalcity
-------------------- --------------------
UK London
USA Washington

and this from the second:

countryname capitalcity
-------------------- --------------------
UK Manchester
USA Washington

(though these results aren't guaranteed - that's part of the problem).

Why did the result change? The physical implementation has affected the
meaning of the code, with serious, potentially disastrous consequences.
How can you even test your code if its results are subject to change
due to the vagaries of storage, indexing and cacheing?

With the ANSI syntax there is no ambiguity. The UPDATE statement
compels the programmer to design an unambiguous assignment subquery
that returns no more than a single value.

UPDATE Countries
SET capitalcity =
(SELECT MIN(cityname)
FROM Cities
WHERE Countries.countryname = Cities.countryname);

At the very least this forces the developer to reconsider whether the
UPDATE statement makes logical sense. You might want to make an effort
to learn Standard SQL instead of a dialect that can change at any time,
which will not port, cannot be understood by other programmers, etc.


[quoted text, click to view]

Re: Error While Updating a Temp Table - Cannot resolve collation c Sevugan
6/27/2005 12:20:03 AM
Hi,

UPDATE A
SET A.Field1= ISNULL(B.Field1, 0)
FROM #TempTable1 A, #TempTable2 B COLLATE .(Put COLLATION...Name here).....
WHERE A.Field2 = B.Field2

Can you please tell me what is Collate and let me know the correct update
statement for the above?

--
Sevugan.C


[quoted text, click to view]
Re: Error While Updating a Temp Table - Cannot resolve collation confl Brian Selzer
6/27/2005 7:20:12 AM
Try this:

UPDATE A
SET A.Field1 = ISNULL(B.Field1, 0)
FROM #TempTable1 A, #TempTable2 B
WHERE A.Field2 COLLATE database_default = B.Field2 COLLATE database_default

COLLATE database_default forces the collation to match the default collation
for the current database. One thing you need to watch out for is case
sensitivity. The default collation for the current database may ignore case
or it may not, and that could give you unexpected results.


[quoted text, click to view]

AddThis Social Bookmark Button