Why am I getting syntax error of bracket? - sql-server

I have the following sql query:
\
(
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ')'.
Everything seems correct. but dont know why this error?

This isn't an answer. Rather it demonstrates what happens after considering all the comments. Here is your query after editing and formatting for readability. Warning - DISTINCT is often a kludge to cover up a logic or schema fault.
select distinct Baan1.t_pono, Baan1.t_oqua, Baan1.t_eono,
Baan1.t_odat, Baan2.t_bano, Baan2.t_orno, Baan2.t_send, Baan3.t_cuno
from BAAN1.baandb.dbo.ttdsls04020 Baan1
join [M3].[v850_Staging] Mthree on
Mthree.PONum1 collate DATABASE_DEFAULT = Baan1.t_eono collate DATABASE_DEFAULT
join BAAN1.baandb.dbo.ttcedi702200 Baan2
<see anything wrong here?>
join [M3].[v850_Staging] Mthree on
Mthree.PONum1 collate DATABASE_DEFAULT = Baan2.t_bano collate DATABASE_DEFAULT
join BAAN1.[baandb].[dbo].[ttcedi010200] Baan3 on
Mthree.Relation collate DATABASE_DEFAULT = Baan3.t_reno collate DATABASE_DEFAULT
<lack of ORDER BY clause is usually a fault>
<I added a statement terminator - which is a good habit you should develop
;
Notice that columns from Mthree are not used in the select list. Notice also that alias MThree is defined TWICE. It is odd that a "staging" table (view?) in one database is used to relate rows between tables in another database. Seems much more work is needed that goes beyond basic syntax issues.

Related

Using CAST, CONCAT and COLLATE with a LEFT OUTER JOIN

I'm trying to CONCAT two columns and also use CAST and COLLATE but keep getting a host of different errors when I try to fix them in a way I think would work.
Basically I am trying to CONCAT two columns together but I get a collation conflict. So then, I try and COLLATE the two columns and I then get a datatype is invalid for COLLATE error. After this I try to CAST the column giving me the error to change it to a varchar but it doesn't work. I'm just unsure how to make all 3 work together.
SELECT TransactionHeader.TransactionType,
TransactionHeader.TicketStub,
CAST ( TransactionHeader.TransactionNumber AS nvarchar(8)) AS [TN],
TransactionHeader.ActualAmount,
Currencies.SwiftCode,
TransactionHeader.CurrencyID,
Divisions.ShortName,
DealHeader.StartDateNumber,
DealHeader.EndDateNumber,
CONCAT (TransactionHeader.TicketStub,
TransactionHeader.TransactionNumber) AS [DealRef]
FROM Company.dbo.TransactionHeader TransactionHeader
LEFT OUTER JOIN Company.dbo.DealHeader DealHeader
ON TransactionHeader.THDealID=DealHeader.DHDealID
LEFT OUTER JOIN Company.dbo.Currencies Currencies
ON TransactionHeader.CurrencyID=Currencies.CRRecordID
LEFT OUTER JOIN Company.dbo.Divisions Divisions
ON TransactionHeader.PrimaryPartyID=Divisions.DVRecordID
WHERE TransactionHeader.TicketStub COLLATE DATABASE_DEFAULT
= TransactionHeader.TransactionNumber COLLATE DATABASE_DEFAULT
All in all, I just want to CONCAT the TicketStub and TransactionNumber Columns but I am not sure how to get past the errors I'm getting. As far as the COLLATE goes I'm still kind of usnsure how it even works, I just know to fix the collation error I need to do it. I am very new to T-SQL and have only been writing it for the past month and a half so please, any advice at all would be very helpful. Thank you!
Collation is a setting that determines how a DB should treat character data at either the server, database, or column level. There's a really good blog on this at red-gate.. Each server, and database, will have a collation. It's common for the databases and server to match, since by default a database will inherit this setting from the model database. It is uncommon to see column level collation, but that seems to be what you have here since all of your tables are coming from the same DATABASE.
You will need to figure out what the collation is on those columns. Dave Pinal has a good write up on this on his blog. You can also do this a few other ways. See the docs for that.
Once you have your collation, you can then collate the CONCAT. It will look something like the below. Here I just use the DATABASE_DEFUALT which would probably work in your case:
CONCAT(TransactionHeader.TicketStub COLLATE DATABASE_DEFAULT,TransactionHeader.TransactionNumber COLLATE DATABASE_DEFAULT) AS [DealRef]
You can find more examples of COLLATE WITH CONCAT in this answer and this one

Cannot resolve collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Arabic_100_CI_AS_KS_WS" in UNION ALL operator [duplicate]

This question already has an answer here:
Cannot Resolve Collation Conflict
(1 answer)
Closed 5 years ago.
i have to databases one of these database is in my server and another one is in another server that i have just read access to it just can select data from tables.
now i have made 2 views, first view is from my data base and it's name is "qryTransmittals_Lines_with_CT_CS"
and second view gets it,s data from other server and other database is (qryTqLines)
now i want to union all data from both queries but i got error:
Cannot resolve collation conflict between "SQL_Latin1_General_CP1_CI_AS"
and "Arabic_100_CI_AS_KS_WS" in UNION ALL operator occurring in SELECT
statement column 8.
SELECT qryTqLines.*
FROM qryTqLines
UNION ALL
SELECT qryTransmittals_Lines_with_CT_CS.*
FROM qryTransmittals_Lines_with_CT_CS;
i checked collation of my table in my server is (SQL_Latin1_General_CP1_CI_AS) and about another table i do not have access to modify design of table.
please help how i can solve my problem.
As i was provided link and OP solved his problem, so posting some code from provided link for future readers
Have to make both column with same Collate so we can compare.
1a. here we change collate of search.cat_id to collate SQL_Latin1_General_CP1_CI_AS from Latin1_General_CI_AS
SELECT search.*
FROM categories INNER JOIN search
ON categories.cid = search.cat_id collate SQL_Latin1_General_CP1_CI_AS
OR
1b. here we change collate of categories.cid to Latin1_General_CI_AS from collate SQL_Latin1_General_CP1_CI_AS
SELECT search.*
FROM categories INNER JOIN search
ON categories.cid collate Latin1_General_CI_AS = search.cat_id
reference link
http://www.ashishblog.com/how-to-resolve-the-collation-conflict-and-how-to-check-collate-in-sql-server/

How to use the COLLATE in a JOIN in SQL Server?

I´m trying to join two tables but I get this error:
Msg 468, Level 16, State 9, Line 8 Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and
"Latin1_General_CI_AS" in the equal to operation.
This is the code I´m using:
SELECT *
FROM [FAEB].[dbo].[ExportaComisiones] AS f
JOIN [zCredifiel].[dbo].[optPerson] AS p
ON (p.vTreasuryId = f.RFC) COLLATE Latin1_General_CI_AS
I know it is wrong, it underlines COLLATE. I do not know how to apply it.
Correct syntax looks like this. See MSDN.
SELECT *
FROM [FAEB].[dbo].[ExportaComisiones] AS f
JOIN [zCredifiel].[dbo].[optPerson] AS p
ON p.vTreasuryId COLLATE Latin1_General_CI_AS = f.RFC COLLATE Latin1_General_CI_AS
As a general rule, you can use Database_Default collation so you don't need to figure out which one to use. However, I strongly suggest reading Simons Liew's excellent article Understanding the COLLATE DATABASE_DEFAULT clause in SQL Server
SELECT *
FROM [FAEB].[dbo].[ExportaComisiones] AS f
JOIN [zCredifiel].[dbo].[optPerson] AS p
ON (p.vTreasuryId = f.RFC) COLLATE Database_Default

Getting wrong results whith specific "WHERE" condition

gurus.
I'm stuck with my problem and will appreciate any help or suggestion. Please check this pic.
I don't understand why I'm getting wrong result in bottom query. As you can see the difference with previuos query is only in "WHERE" clause, but this difference must lead to the same results since it's one-to-one join.
Important thing is that v_last_part_info is as a view and I changed it recently. I thought it's due to QEP cache, but i tried OPTION (RECOMPLIE) and even solution described here. The result is still same.
Please, help! What am I missing?
P.S.: [OBJECT_ID] is a column name, not built-in function
P.P.S: ANOTHER_DB has different collation that's the reason i need collate database_default
select Tracking
, SoItem
from v_last_part_info
where Tracking = '4170664293'
Tracking SoItem
4170664293 20
--================================================================================--
select
lpi.Tracking
, lpi.SoItem
from v_last_part_info lpi
join ANOTHER_DB..SO_HEADER h on lpi.Tracking = h.[OBJECT_ID] collate database_default
where Tracking = '4170664293'
Tracking SoItem
4170664293 20
--================================================================================--
select
lpi.Tracking
, lpi.SoItem
from v_last_part_info lpi
join ANOTHER_DB..SO_HEADER h on lpi.Tracking = h.[OBJECT_ID] collate database_default
where [OBJECT_ID] = '4170664293'
Tracking SoItem
4170664293 10
Thanks to GarethD I found out the reason. This happened because of row_number() function inside v_last_part_info. Accorgin to the definition at MSDN:
There is no guarantee that the rows returned by a query using
ROW_NUMBER() will be ordered exactly the same with each execution
unless the following conditions are true.
Values of the partitioned column are unique.
Values of the ORDER BY are unique.
Combinations of values of the partition column and ORDER BY columns are unique.
In my case option 2 was not secured.

Cannot resolve the collation conflict between "Latin1_General_CI_AI" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation

I am trying to create a procedure in SQL Server 2008R2 but it is showing this error
Cannot resolve the collation conflict between "Latin1_General_CI_AI"
and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
The procedure that I have created is
CREATE Procedure Ps_Quiz_OnlineTest_QuestionsWithOptions_Get --'Ques1'
#Ques Varchar(8000)
As
Begin
Select
A.QuestionId,
A.QsnDesc,
A.CorrectOption,
B.OptionValue,
A.Marks,
(
Select QsnName
From Quiz_tblQsnsLimitMaster
Where QsnId = #Ques) QuesPaper,
(
Select Durationoftest
From Quiz_tblQsnsLimitMaster
Where QsnId = #Ques) QuesPaper
From
Quiz_tblQsnCreationMaster A,
Quiz_tblQsnCreationDetail B
Where
A.QuestionId = B.QuestionId
And A.QuestionId In (
Select QuestionIds
From FN_Question_Answers_Quiz(#Ques))
And B.QuestionId In (
Select QuestionIds
From FN_Question_Answers_Quiz(#Ques))
Order By
A.QuestionId,
B.OptionOrder
End
I tried to collate tables with different collations but it did not worked.
How can I solve this.
probably this might help, change your where condition like this
Where A.QuestionId COLLATE DATABASE_DEFAULT = B.QuestionId COLLATE DATABASE_DEFAULT
Since you've mentioned that QuestionID in each table is varchar, it's probably the comparison between those. So at a guess, try changing the line:
Where A.QuestionId = B.QuestionId And
To the following:
Where A.QuestionId = B.QuestionId COLLATE SQL_Latin1_General_CP1_CI_AS And
I'd prefer it if you used proper join syntax for your tables - but that's for another day ;-)
try use this,
where fieldname COLLATE DATABASE_DEFAULT = secondfieldname COLLATE DATABASE_DEFAULT
this is working
Just use the following syntax when joining tables with different collations,
where A.QuestionId collate SQL_Latin1_General_CP1_CI__AS = B.QuestionId collate
SQL_Latin1_General1_General_CP1_CI_AS
To overcome this use the below code:
SELECT NAME
FROM sys.objects
WHERE NAME COLLATE DATABASE_DEFAULT NOT IN (SELECT TYPE
FROM sys.objects)
I was facing same issue after migration of Database and shifting of server by service provider. So as solution I have created new database using CPanel then execute SQL query for schema and data to restore. Finally this issue got resolved.

Resources