microsoft sql select query from multiple tables - sql-server

This query is working in mysql but is not working in microsoft sql server management studio 2008, can someone help me out?
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM cms_question_report QR,
cms_clients C,
cms_questioncategory QC ,
cms_reporthistory RH
WHERE C.id=QR.cid
AND QR.rhid=RH.id
AND QR.catid='3'
AND QR.catid=QC.id
I am getting the error: Invalid object name cms_question_report

SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM cms_question_report QR
left join cms_clients C
on C.id=QR.cid
left join cms_questioncategory QC
on QR.catid=QC.id
and QR.catid='3'
left join cms_reporthistory RH
on QR.rhid=RH.id
I think this should do

specify Normally it happens when you have specific schema and you don't specify it for example:
Replace dbo. with your schema and/or type your database name
SELECT DISTINCT C.firstname,C.lastname,QC.category_name,QR.cid,QR.catid,QR.rhid
FROM databasename.dbo.cms_question_report QR,
databasename.dbo.cms_clients C,
databasename.dbo.cms_questioncategory QC ,
databasename.dbo.cms_reporthistory RH
WHERE C.id=QR.cid
AND QR.rhid=RH.id
AND QR.catid='3'
AND QR.catid=QC.id

Related

SQL Alternative for FOR XML PATH

For some reason my query :
SELECT
MP_SQLQRY_ProjectArchive.ProjectShort,
MP_SQLQRY_ProjectArchive.ProjectName ,
MP_SQLQRY_ProjectArchive.ProjectManagerName ,
ProjectOwnerName,
ProjStateName,
ProjectKategoryShort,
ProjectTypeShort,
MP_SQLQRY_ProjectArchive.ProjectPriority,
DepartmentShort,
ProjectArchivedDate,
CONVERT(NVARCHAR(MAX),ERPAccountCodes.ERPAccountCodes) As ERPAccountCodes
FROM
MP_SQLQRY_ProjectArchive
INNER JOIN
SQLQRY_VALID_PROJECT_ORDER_INFO ON MP_SQLQRY_ProjectArchive.ProjectID = SQLQRY_VALID_PROJECT_ORDER_INFO.ProjectID
INNER JOIN
COM_tbl_MAD_Project ON MP_SQLQRY_ProjectArchive.ProjectID = COM_tbl_MAD_Project.ProjectID
LEFT JOIN
(SELECT
ProjectID,
CONVERT(Nvarchar(max), STUFF((SELECT ';' + [SAPNumber] AS [data()]
FROM [COM_tbl_MAD_ProjectERPAccountCode] t2
WHERE t2.[ProjectID] = t1.[ProjectID]
ORDER BY [SAPNumber]
FOR XML PATH('')), 1, 1, '')) AS ERPAccountCodes
FROM
[COM_tbl_MAD_ProjectERPAccountCode] t1
GROUP BY
[ProjectID], [SAPNumber]) AS ERPAccountCodes ON ERPAccountCodes.ProjectID = COM_tbl_MAD_Project.ProjectID
will not return the values for the ERPAccountCodes column. In SQL Server, it works fine and I get my values, which are seperated with a ; as I want.
But when I run it as extern connection with ADODB in access this single column is empty.
I suppose it is a problem with access or ADODB but I have no clue what to do about it
Is there an alternative to FOR XML PATH or is there a different way to do it?
For clarification, the values return some blank values. Not NULL, just these weird blank symbols when i debugged in VBA in access, altough I tried converting it into a NVarchar which theoretically should work just fine.
Left is an Excel created from the MS Access SQL call. Right is from SQL Server Management Studio:

Query System Views From Linked Server - SQL Server

I'm tasked with creating a report that will pull down the permissions from different servers and display them.
I'm having an issue with the query not picking up all of the rows in from the system view, from another server.
When I run the below query on serverA, it gives me 251 results.
SELECT COUNT(*) FROM ServerA.employee.sys.objects
When I run the same code from ServerB, I get 153 results.
I compared the two and it looks like the linked server isn't pulling type_desc of SQL_SCALAR_FUNCTION, SQL_STORED_PROCEDURE, and SYSTEM_TABLE.
Does anyone know way I can get a list of database object permissions running from a different server in SQL server?
If you are using SSRS for the report, you can setup a dynamic data source to run the same report for multiple servers/databases. I'd use a static data source at first to setup the datasets, otherwise the columns will not get created automatically.
="Data Source=" & Parameters!ServerName.Value & ";Initial Catalog=" & Parameters!DatabaseName.Value
Here is the listing of permissions on schema objects within a database.
SELECT
pr.principal_id
, pr.name
, pr.type_desc
, pr.authentication_type_desc
, pe.state_desc
, pe.permission_name
, ObjectName = s.name + '.' + o.name
FROM
sys.database_principals AS pr
INNER JOIN sys.database_permissions AS pe ON pe.grantee_principal_id = pr.principal_id
INNER JOIN sys.objects AS o ON pe.major_id = o.object_id
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id;
Microsoft Reference

SQL JOIN AS400 from table #temp

I want to make a dynamic join between sql and as400, of this type:
SELECT * FROM OPENQUERY(AS400_link,'SELECT
AS400.CAMPO1
,AS400.CAMPO2
,AS400.CAMPO3
,AS400.CAMPO4
FROM AS400_FILE AS AS400
INNERT JOIN #TAB_TEMP AS TEMP ON
TEMP.CAMPO1 = AS400.CAMPO1
')
DROP TABLE #TAB_TEMP
if I run it:
OLE DB provider "IBMDASQL" for linked server "AS400_link" returned message "SQL0113: Nome #TAB_TEMP non consentito.
Causa . . . : #TAB_TEMP...
some solution?
Thank you
I think what you're looking for is more like
SELECT * FROM OPENQUERY(AS400_link,'SELECT
AS400.CAMPO1
,AS400.CAMPO2
,AS400.CAMPO3
,AS400.CAMPO4
FROM AS400_FILE') AS AS400
INNER JOIN #TAB_TEMP AS TEMP ON
TEMP.CAMPO1 = AS400.CAMPO1
Don't expect it to perform well.

got unexpected value in TDS response at offset:0

I'm using JDBC Driver Version 1.2.2828.100 and Database Sql Server 2008 R2.
I'm doing search operation by joining three tables containing 1,40,000 rows
SELECT USERID, USERLOGIN, DOMAIN FROM USER (nolock), USER01 (nolock), DOMAIN (nolock) WHERE USERLASTNAME= "aa" AND USERFIRSTNAME = "bb" AND USERDOMAINID = DOMAINID AND USERID = USER01ID ORDER BY USERID
I'm getting the below Error and my application crashes immediately
com.microsoft.sqlserver.jdbc.TDSReader throwInvalidTDS
SEVERE: TDSReader#d5ccfa ( ConnectionID:11 TransactionID:0x0000000000000000) got unexpected value in TDS response at offset:0
What does the TDS reponse at offset:0 refers ?
How to resolve this ?
Okay, TDS is tabular data stream. It is how the client (machine) talks to the database (server). You are using the microsoft jdbc driver.
Here are some things to improve your code and fix the problem.
1 - Please use ANSI 20xx standards.
That means the query will be re-written as the following.
-- Updated using newer syntax
SELECT
U0.USERID,
U0.USERLOGIN,
D0.DOMAIN
FROM
USER as U0 with (nolock) INNER JOIN
USER01 as U1 with (nolock) ON U0.USERID = U1.USER01ID INNER JOIN
DOMAIN as D0 with (nolock) U0.USERDOMAINID = D0.DOMAINID
WHERE
U0.USERLASTNAME= "aa" AND
U0.USERFIRSTNAME = "bb"
ORDER BY
U0.USERID
I no not advocate the (nolock) but that is a whole different battle.
2 - Next, look at this article. The user found out that a type case was causing the issue. Make sure the data types are compatible with JAVA.
http://adrielservice.com/blog/?p=362
I hope this helps.
John

CodeIgniter ActiveRecord join statement dropping '[' from the ON clause

Using Codeigniter (v1.7.2) Active Record Class to query a MSSQL Database (external DB, I don't control table or field names)
The following Code:
$this->db->select('[Key Field], MemberInfo.OtherField');
$this->db->limit(10);
$this->db->from('primaryTable');
$this->db->join('MemberInfo', '[Member Number] = Member_Number', 'left');
$this->db->where('[Member Number] = 573');
$this->db->where('[Incident Date] BETWEEN '. $DateStart . ' AND ' . $DateEnd);
Produces the following Query (which has a syntax error)
SELECT TOP 10 [Key Field], MemberInfo.OtherField
FROM primaryTable
LEFT JOIN MemberInfo ON Member Number] = Member_Number
WHERE [Member Number] = '573'
AND [Incident Date] BETWEEN 2012-01-01 AND 2012-07-19
Note the JOIN clause, which is missing the opening '[' on the field name
I tried using double quotes, which yielded the same result.
Anyone have ideas, is this a known bug in 1.7.2 (which I know is old, working up updating)?
UPDATE
issue is present in 2.1.2 also
Accepted answer below is also the fix for 2.1.2: change the regex check in the JOIN function to look for the opening [
The problem is in the parameter ON. There does not exist an integrated solution in the framework (not in version 2).
You have 2 options:
Altering the core of the library active_record
Write the query in a complete chain with concatenations using $this->db->query();

Resources