Cross-database queries with numbered database name - sql

I'm a bit of a novice when it comes to SQL Server 2005. I have a database containing most of the stored procedures and tables (we'll call it 'GrandDatabase'). Each user has its own separate database named after the user's numbered ID. So I have a database list as follows, for example:
GrandDatabase
100
101
102
...
I need to join tables across the GrandDatabase and a user's database. I've read elsewhere that the following should work, when executed from GrandDatabase:
SELECT
*
FROM
GrandDatabase.User INNER JOIN
100.dbo.UserInfo ON GrandDatabase.User.UserID = 100.dbo.UserInfo.UserID
This gives me a syntax error, complaining about the '.' right after the first reference to the 100 database. I did a little tweaking and discovered that this code works fine when I use non-numbered databases (for instance, replacing the '100' above with 'User100'). Does anybody know how to make this work with numbered database names?
Thanks!
Chris

Try using [100].dbo.UserInfo instead of just the 100.

Try putting the numbers into square brackets and using aliases, e.g.:
SELECT *
FROM GrandDatabase.User
INNER JOIN [100].dbo.UserInfo u
ON GrandDatabase.User.UserID = u.UserID

Try enclosing the database name with brackets:
SELECT
*
FROM
GrandDatabase.User INNER JOIN
[100].dbo.UserInfo ON GrandDatabase.User.UserID = [100].dbo.UserInfo.UserID

Related

Filtering on a derived column in UniOLEDB

I am working on a piece of SQL for a IBM U2 Rocket database. It's not a flavour of db platform I'm familiar with.
I do not have direct access to this database: I can only access it by composing statements in the calling code and testing it from there. That makes it kind of awkward to figure out where the problem is when there are syntax errors.
I'm trying to compose a query which has a condiational on a dervied column. As far as I can tell from what I've read about the database, this ought to work:
SELECT a.*
FROM (
SELECT dp.NAME
,dp.Code
,dp.BusinessType + ts.BusinessType AS bType
FROM dataPoints dp
LEFT OUTER JOIN trialSuppliers ts
WHERE ts.AccountStatus = 'A'
) a
WHERE a.bType LIKE '%cho%'
However, it throws this error:
Died in UCI::SQLExecDirect() with SQLSTATE 37000, Native error:0
[IBM][SQL Client][UNIDATA]
If you just run the inner query, it works fine. Trying to use any kind of inner select statement causes it to throw the same error, i.e. this:
SELECT *
FROM (
SELECT dp.NAME
,dp.Code
,dp.BusinessType + ts.BusinessType AS bType
FROM dataPoints dp
LEFT OUTER JOIN trialSuppliers ts
WHERE ts.AccountStatus = 'A'
)
Still fails.
What's the correct syntax to be able to filter my query by the derived column?
Just doing some clean-up, did the last comment resolve the issue?
I am not a UniData user but in its half cousin UniVerse the "from clause" has to be a table. You have to do sub-queries in the where clause. I would do what you want to do adding a couple of I-descriptors in the dictionary. I have often found the limited SQL compliance of the U2 products to be somewhat of a hindrance, but when your metadata is external to and separate from your data you are going to lose a bit of structured query sanity. – Van Amburg Sep 21 '17 at 17:31

Adding multiple rows of data into one - TSQL

I'm currently working on a project of my own that uses an SQL DB to store character information for an RPG game. I'm trying to write a select statement to retrieve the information related to the equipment the user is using, at the moment I have:
SELECT
CE.CharacterID, CE.EquipmentSlotID, CE.ItemID, I.ItemTypeID, I.Name, I.Image, IT.Name, IT.Description, concat(IA.AttributeID, ' ', IA.value )
FROM
CharacterEquipment CE INNER JOIN Item I
ON CE.ItemID = I.ItemID
INNER JOIN ItemType IT
ON I.ItemTypeID=IT.ItemTypeID
INNER JOIN ItemAttribute IA
ON I.ItemID=IA.ItemID
WHERE CE.CharacterID = 1
ORDER BY CE.ItemID ASC;
This returns this:
My problem is that each item (ItemID) can have multiple attributes (AttributeID) and I want all these attributes to appear on a single line but I'm not sure how I'd go about combining the results
EDIT:
Sorry guys, should have said, I've been experimenting with the group concat function but SQL isnt my strong suit so im having bother writing it and figuring out how it works, any chance any one could explain it abit more indepth? Thanks
Possible solution to this would be improving your database structure and try denormalizing it.
2.But if you wish to continue with same structure try using Concatenation using COALESCE function
For your reference :Concatenate many rows to one

Using SQL to create table joins in MS Access

I'm REALLY new to SQL and I'm really struggling with all but the simplest of joins - especially in MS Access.
At this stage, all I want to do is create a query from two tables: 'tblUsers', with columns 'UserID' and 'User', and 'tblPayments' with columns 'PaymentID', 'User' and 'Authoriser'. I want my query to contain all of this data and also to have columns 'UserID' and 'AuthoriserID', both of these ID numbers being taken from 'tblUsers', but clearly one will relate to the User and one to the authoriser.
I'm sure this is far more simple than I'm making it but how should I do this?
Thanks in advance.
Keep in mind that there are minor differences in syntax between SQL server SQL and Access SQL. While in most cases a query written in Access will function normally on SQL server, the opposite is less true because of the shortcuts often used on SQL server (example: dropping the 'INNER' from the joins and the 'AS' when using an alias.)
Access:
SELECT tblUsers.UserID,
tblUsers.User,
tblPayments.paymentID,
tblUsers.User AS Authoriser,
tblUsers_1.UserID AS AuthoriserID
FROM (tblPayments
INNER JOIN tblUsers AS tblUsers_1
ON tblPayments.AuthorisorID = tblUsers_1.UserID)
INNER JOIN tblUsers
ON tblPayments.userID = tblUsers.UserID;
SQL Server:
SELECT tblUsers.UserID,
tblUsers.User,
tblPayments.paymentID,
tblUsers.User Authoriser,
tblUsers_1.UserID AuthoriserID
FROM tblPayments
JOIN tblUsers tblUsers_1
ON tblPayments.AuthorisorID = tblUsers_1.UserID
JOIN tblUsers
ON tblPayments.userID = tblUsers.UserID;

Compare two tables and find matches in ms-access 2007

I am trying to compare two tables in ms access 2007 to find matches from the tables which I know to have one field in common. When I try to run the following query
SELECT
FROM FirstTrial INNER JOIN ConsolidatedDatabase
ON FirstTrial.ModelNumbr = ConsolidatedDatabase.ModelNumbr;
I get an error 'Query must have at least one destination field'
What do I need to change in order to make the query work?
You need some fields:
SELECT *
FROM FirstTrial INNER JOIN ConsolidatedDatabase
ON FirstTrial.ModelNumbr = ConsolidatedDatabase.ModelNumbr;
It would be better to put some names instead of *
In MS Access, you can use the query design window.

Object Relational Mapping Issues: Suggestions needed

I've been trying to come up with a good design pattern for mapping data contained in relational databases to the business objects I've created but I keep hitting a wall.
Consider the following tables:
TYPE: typeid, description
USER: userid, username, usertypeid->TYPE.typeid, imageid->IMAGE.imageid
IMAGE: imageid, location, imagetypeid->TYPE.typeid
I would like to gather all the information regarding a specific user. Creating a query for this isn't too difficult.
SELECT u.*, ut.*, i.*, it.* FROM user u
INNER JOIN type ut ON ut.typeid = u.usertypeid
INNER JOIN image i ON i.imageid = u.imageid
INNER JOIN type it ON it.typeid = i.imagetypeid
WHERE u.userid = #userid
The problem is that the field names collide and then I'm forced to alias every single field which gets out of hand very quickly.
Does anyone have a decent design pattern for this kind of thing?
I've thought about retrieving multiple results from a single stored procedure and then using a dataset to iterate through each one but I'm worried that some performance issues might bite me in the butt later. For example instead of the above query something like:
SELECT u.*, t.* FROM user u
INNER JOIN type t ON t.typeid = u.usertypeid
WHERE u.userid = #userid;
SELECT i.*, t.* FROM image i
INNER JOIN type t ON t.typeid = i.imagetypeid
INNER JOIN user u ON u.imageid = i.imageid
WHERE u.userid = #userid;
Does that seem like a decent solution? Can anyone foresee any issues with this approach?
Never use the SQL * wildcard in production code. Always spell out all the columns you want to retrieve.
Then aliasing some of them doesn't seem like such a huge amount of extra work.
Re your comment asking for background and reasoning:
Sometimes you don't really need every column from all tables, and fetching them can be needlessly costly (especially for large strings and blobs). There is no SQL syntax for "all columns except the following exceptions."
You can't alias columns that you fetch using the wildcard. Once you need to alias any of the columns, you need to expand the wildcard to list all the columns explicitly.
If the table structure changes, e.g. columns are renamed, reordered, dropped, or added, then the wildcard fetches them all, by position as defined in the tables. This may seem like a convenience, but not when your application depends on columns being in the result set by a given name or in a given position. You can get mysterious bugs where your application displays columns in the wrong order (if referencing columns by position), or shows them as blank (if referencing columns by name).
However, if the SQL query names columns explicitly, you can employ the "Fail Early" principle. This helps debugging, because it leads you directly to the SQL query that needs to be edited to account for the schema change.