Showing posts with label statement. Show all posts
Showing posts with label statement. Show all posts

Tuesday, March 20, 2012

Query Error: Collation Conflict (on tables on 2 different servers)

Hi all,
I have a query that I need to run where I join two tables that bothreside on different servers. I use an INNER JOIN statement to attemptto join these tables, but for some reason I am getting the followingerror message...
"Cannot resolve collation conflict for equal to operation."
The query is as follows...
SELECT TABLE_NAME, LEFT(TABLE_NAME, CHARINDEX('_', TABLE_NAME + '_') - 1) AS Abbreviation
FROM mrcsmis.INFORMATION_SCHEMA.TABLES
INNER JOIN DEVELCAD1.Portal.dbo.dnl_db_names_log AS imp ON (imp.dnl_table_name = TABLE_NAME)
WHERE (TABLE_TYPE = 'BASE TABLE')
Thanks
Tryst

SQL JOINs are from Rene Descartes math so you can only create INNER JOIN if the two tables are equal, if they are not equal you use OUTER JOIN. The other problem the error did not show is you need LINKED Server to run your query so your tables maybe equal to qualify for INNER JOIN but SQL Server is not seeing that because the servers are not linked. Try the links below to get started, the Microsoft link have the System stored procs you need to create LINKED Server. Hope this helps.
http://www.databasejournal.com/features/mssql/article.php/3085211

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adminsql/ad_1_server_4uuq.asp

|||Hi Caddre, thanks for the reply.
I did manage to run the sp_addlinkedserver stored procedure to create a linked server instance.
The problem may be the column types between the two tables.
I have now tried using a LEFT OUTER JOIN, but still get the same error message...
SELECT TABLE_NAME, LEFT(TABLE_NAME, CHARINDEX('_', TABLE_NAME + '_') - 1) AS Abbreviation
FROM mrcsmis.INFORMATION_SCHEMA.TABLES
LEFT OUTER JOIN DEVELCAD1.Portal.dbo.dnl_db_names_log AS imp ON (imp.dnl_table_name = TABLE_NAME)
WHERE (TABLE_TYPE = 'BASE TABLE')
Tryst
|||

It is Column collation related try these links to resolve it. Hope this helps.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_da-db_7ory.asp

http://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=8818馠

Friday, March 9, 2012

Query Dbt

I have a dbt da in sql statement for Access

I have a table with

Production table with fields(Date1,FILE,MT_ID,NAME,LINE_COUNT,QUALITY)
This table has got lots of entries for a particular date itself
Example : 1/6/2003 456777887 4023 SINDHU 89.0 98%
1/6/2003 78889899 4023 SINDHU 78 95%

Now I want to create a seperate table with the fields

DATE -- which has just the unique dates in them
NAME-unique user
MT_ID --unique ID
TOTAL LINE COUNT -- The total line count for unique employees for unique date
AVERAGE ACCURACY -- The average accuracy for unique employee for unique date
LINECOUNT BELOW 98% -- Total No. of Line Count for the unique employee for unique date whose ACCURACY IS BELOW 98%

So for this i wrote a query which gave me

UNIQUE DATE
UNIQUE NAME
TOTAL LINE COUNT
AVERAGE ACCURACY

This is the query

SELECT DATE1,NAME,SUM(LINE_COUNT) AS LCOUNT,AVG(QUALITY) AS ACCURACY FROM Production GROUP BY DATE1,NAME

but i need the MT_ID and LINECOUNT BELOW 98% too

Pls let me know how the subquery will beokay, you realize that for each DATE1,NAME combination, there will be multiple rows, so which value of MT_ID do you want? or is it a new field?

also, i don't understand LINECOUNT BELOW 98% -- could you please explain it again?

rudy
http://r937.com/|||Table1 -- Production

fields(Date1,FILE,MT_ID,NAME,LINE_COUNT,QUALITY)

DATE FILE MT_ID NAME LINE COUNT ACCURACY
1/6/2003 456777887 4023 SINDHU 89.0 98%
1/6/2003 78889899 4023 SINDHU 78 95%
2/6/2003 67899 2344 RETA 789 100%
.....................
............

Now I want to insert into the table --Daywise_Report

FIELDS(DATE1 , MT_ID , Name , TLine_Count,AV_Accuracy, LCB_98)

Where we have to find the total line count and average accuracy for each employees for each date and insert into TLine_Count and AV_Accuracy

And also find the LCB_98 ie -- TOTAL(LINE_COUNT) who's ACCURACY is less than 98%

And insert to appropriate fields
Hope U can help me out
Thanks
regards
Smitha|||if there are multiple MT_IDs in each group, you need to decide how to choose just one of them, i.e. which one

in addition, i still don't understand which lines in each group should be counted if the group's accuracy is less than 98% -- all the lines in a group go towards calculating the group average, no?

i am afraid this is as far as i can help you --

insert
into Daywise_Report
( DATE1
, MT_ID
, Name
, TLine_Count
, AV_Accuracy
, LCB_98 )
select DATE1
, MT_ID
, NAME
, SUM(LINE_COUNT)
, AVG(QUALITY)
, 0
from Production
group
by DATE1
, NAME

Query Data using multiple select statement

Hello everyone!

Is this statement is possible? here is the code:

select sum(nPassengers) Planned, Actual, (sum(Planned) - Actual) Variance
from (
select iDeptCode, sum(nPassengers) Planned from tbl_BusRequest
where iReqTime = '1' and iDeptCode = '1' and cReqType = 'I'
group by iDeptCode
and
(Actual = (Select nPassengers from tbl_Riders_NonRiders where dtDate = '11/05/06'
AND cReqType = 'i' and iTimeCode = '1' and iDeptCode = '1' )

and im getting this error:

Server: Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'and'.

Thankssee your previous post for the possible solution|||

Quote:

Originally Posted by intscript

Hello everyone!

Is this statement is possible? here is the code:

select sum(nPassengers) Planned, Actual, (sum(Planned) - Actual) Variance
from (
select iDeptCode, sum(nPassengers) Planned from tbl_BusRequest
where iReqTime = '1' and iDeptCode = '1' and cReqType = 'I'
group by iDeptCode
and
(Actual = (Select nPassengers from tbl_Riders_NonRiders where dtDate = '11/05/06'
AND cReqType = 'i' and iTimeCode = '1' and iDeptCode = '1' )

and im getting this error:

Server: Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'and'.

Thanks


You may want to put the results of the second 1/2 of your query in a temp table, then select from there.

A bit easier to read, if nothing else

Saturday, February 25, 2012

QUERY BY USER

I have a datagrid with rows that pertain to multiple users. I want to
write a query statement so that whomever the CURRENT logged-in
user is, he can go to the datagrid page and it will show ONLY rows
that pertain to him (programmatic security). To accomplish this,
I have a column in the datatable that references each user's
username. So I need a query statement that will compare the table's
'username' column to the current logged-in user. Something like...

SELECT... FROM [VendorEquipment]
WHERE [UserName] = Context.Current.User.Name

...or something like this. Question is, can someone help on how to
syntax this. Also, do I need anything special in the login form or in
web.config to 'pave the way' for this query to work.

Thx much.
Reid C.after a successfull login,you can store the username in sessions...|||Thanks for the reply...but I am a novice and will probably need more to clear up
the picture...can you suggest how I can syntax my SELECT command to accomplish
the above-stated objective and is there something I need to do via sessions to
enable this--and where would I do that?

Thx much.
Reid C.|||i believe you are checking for user authorization when the user logs in right ?

just put the username into session after the validation is successfull


session("username")=username

and frm any page
you can get it back as

dim uname as string
uname=session("username")

so when you query the db for the records you can do something like

strsql=select * From table where username='" & uname & "'"

and bind the datagrid.

HTH|||Thanks again...this sounds like what I'm looking for...just two
more questions due to my relative inexperience:

1) the statement: session("username")=username
you suggested...does that need to be in the login form
like in the "sub loginBtn_click" subroutine? and if so where in
the subroutine does it need to go...is the placement of the
statement critical?

2) is "username" interchangeable in this operation with "userid" ?
because I have written my registration form and login with a
"userid" instead of a "username"...but I will change this if it is
important to have "username" for forms authentication.

Thx.
Reid C.|||yep you can have any variable in session you want..username,userid...doesnt matter.
right after you check the userid with the password and if the query returns as a valid user, store the userid in sessions...and you should be able to access it frm any page...

HTH|||Thank you ...do I place the session stmt in global.asax?

like this, for instance:

Sub Session_OnStart()
Session("userId")=userId
End Sub


?

Thx.
Reid C.|||no...you put it in ur login.aspx where ever you validate the authenticity of the user...|||o.k., thanks again, ...but I am a complete novice and have been building pages
using WebMatrix, so sorry to be dense...but do I place the "session..." statement
in the "Function GetUser..." or in my "Sub LoginBtn_click..." subroutine, or after
that and right before the </script> tag ? I know placement of this statement is
important, I just don't know where?

Thx again.
Reid C.|||if you post your code for the login page..i can tell you where to place it...|||Thanks again for all your help...here is the code for the login (minus the html) and
I've got the statement placed right before the end </script> tag...tell me if you see
a glaring case of needing to move it to a more appropriate space.


<script runat="server">
Function GetUser(ByVal userId As String, ByVal userPassword As String) As System.Data.DataSet
Dim connectionString As String = "server='(local)'; trusted_connection=true; database='VSdatastore'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim queryString As String = "SELECT [Users].* FROM [Users] WHERE (([Users].[UserId] = @.UserId) AND ([Users].[U"& _
"serPassword] = @.UserPassword))"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_userId As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_userId.ParameterName = "@.UserId"
dbParam_userId.Value = userId
dbParam_userId.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_userId)
Dim dbParam_userPassword As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_userPassword.ParameterName = "@.UserPassword"
dbParam_userPassword.Value = userPassword
dbParam_userPassword.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_userPassword)

Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)

Return dataSet
End Function
Sub LoginBtn_Click(Sender As Object, E As EventArgs)

If Page.IsValid Then
Dim userDS As New System.Data.DataSet
userDS = GetUser(UserId.Text, UserPassword.Text)
If userDS.Tables(0).Rows.Count = 1 Then
FormsAuthentication.RedirectFromLoginPage(UserId.Text, false)
Else
Msg.Text = "Invalid UserId or Password: Please try again"
End If
End If

End Sub
session("UserId")=UserId
</script


Thx again.
Reid C.|||i think this should work...

If Page.IsValid Then

Dim userDS As New System.Data.DataSet

userDS = GetUser(UserId.Text, UserPassword.Text)

If userDS.Tables(0).Rows.Count = 1 Then
'valid user...
session("UserId")=UserId

FormsAuthentication.RedirectFromLoginPage(UserId.Text, false)

Else

Msg.Text = "Invalid UserId or Password: Please try again"

End If

End If

End Sub

session("UserId")=UserId

|||In your revision you have the "session..." statement right after
the "If userDS.Tables(0).Rows.Count = 1 Then" statement which
made sense as soon as I saw it. But you also have the same
"session..." statement again after the "End Sub" statement. Is
this just a typo on your part from copying and pasting what I
sent you, or does it need to be there?

I am assuming it is a copy&paste oversight...unless you answer
back that it needs to be repeated...

Thx much again for your help.
Reid C.|||See if you can review the code for my datagrid page and see where I'm going wrong.
One thing, the "UserId" I need to compare is in the datatable as "PropUserId" because
I also have a column in the table "VendorUserId" and need to reference both. So I
need to compare the session "UserId" with the table's "PropUserId" column and I've
added a statement in the "Function GetVendorEquipment..." that says the "PropUserId"
column is equivalent to "UserId"...but I am getting a blank table on the page when I
run it.


<script runat="server"
Function GetVendorEquipment() As System.Data.DataSet
Dim connectionString As String = "server='(local)'; trusted_connection=true; database='VSdatastore'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)
dim UserId as String
dim PropUserId as String
PropUserId=session("UserId")
Dim queryString As String = "SELECT [VendorEquipment].* FROM [VendorEquipment] WHERE [PropUserId]='" & UserId & "'"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)

Return dataSet
End Function

Sub Page_Load(sender As Object, e As EventArgs)
DataGrid1.DataBind()
End Sub

</script>

Thx again for your help.
Reid C.|||ur userid seems to be a number...
so you have to use


Dim queryString As String = "SELECT [VendorEquipment].* FROM [VendorEquipment] WHERE [PropUserId]=" & PropUserId

also one other suggestion...bind your datagrid within if not ispostback...loop


Sub Page_Load(sender As Object, e As EventArgs)
if not ispostback then
DataGrid1.DataBind()
end if
End Sub

HTH

query by recent dates

How would I construct a SQL statement that would query the database and return the 5 most recent dates added into the database?

Would I use the TOP SQL keyword to select the 5 most recent entries?

How would I query for the most recent dates?

SELECT TOP 5 *
FROM dbo.tblWeblog
WHERE blogDate = ?

Thanks for any help!
-Dman100-Close! I'd use:SELECT TOP 5 *
FROM dbo.tblWeblog
ORDER BY blogDate DESC-PatP|||Thanks Pat!
-Dman100-

Monday, February 20, 2012

Query and Collation problem

I am querying against a Linked Server, the following statement:
SELECT * FROM Table
WHERE name_field LIKE 'José%'
The "name_field" field, is a varchar(50) with SQL_Latin1_General_CP1_CI_AI...
it's Accent Insensitive, what is right... So, when I send this query, I
receive all the 'Jose' and 'José' regs...
I have a site that does the same query, from another server, using linked
server resource... The query is almost the same:
SELECT * FROM [Server].Database.Owner.Table
WHERE name_field LIKE 'José%'
This time, looks like it's using Accent Sensitive, because it don't bring
just the 'José' regs...
It's an Asp app who is querying this way...
Does someone know what can It be?
Thx,
Rafa®What SQL Server versions are these - the ones being used in the Linked
Server?
Vikram Jayaram
Microsoft, SQL Server
This posting is provided "AS IS" with no warranties, and confers no rights.
Subscribe to MSDN & use http://msdn.microsoft.com/newsgroups.