Query too long in .NET 3.5 and SQL Compact 3.5 - sql-server

this is a rather pathetic problem:
In Visual Basic 2008 Express with SQL Server Compact 3.5 and the Usual DataSet / TableAdapters, My Query is too long:
Changing the names, it is a query like this:
SELECT Table1.*, Table3.*
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.T1ID
INNER JOIN Table3
ON Table2.T3ID = Table3.PK
Problem is, Table1 and Table3 have around 10 and 5 columns each, with rather descriptive names, and The Table adapter is insistent on writing all the columns out and therefore hacks off my command. (It won't take * 's, it always says it can't find the column Table1.* )
Is there a way around this?

If you add an alias for the tables, will the table adapter take it?
SELECT t1.*, t3.*
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.ID = t2.T1ID
INNER JOIN Table3
ON t2.T3ID = t3.PK

Put the query in a stored procedure and then just call it.
(It's also cleaner, more efficient, and helps encapsulate the logic within the database instead of your application just doing anything it likes)

Related

MS SQL Server trouble with JOIN

I'm new to SQL and need a push in the right direction.
I currently have a working SQL query accessing 3 tables in a database, and I need to add a JOIN using a 4th table, but the syntax escapes me. To simplify, what I have now is:
SELECT
t1_col1, t1_col2, t2_col1, t2_col2, t3_col1
FROM
table1, table2, table3
WHERE
{some conditions}
ORDER BY
t1_col1 ASC;
What I need to do is to add a LEFT OUTER JOIN selecting 2 columns from table4 and have ON t1_field1 = t4_field1, but whatever I try, I'm getting syntax errors all over the place. I don't seem to understand the correct syntax.
I tried
SELECT *
FROM table1
LEFT OUTER JOIN table2;
which has no errors, but as soon as I start SELECTing columns and adding conditions, I get stuck.
I would greatly appreciate any assistance with this.
You do not specify the join criteria. Those would be in your WHERE clause under "some conditions". So, I will make up so that I can show syntax. The syntax you show is often termed "old". It has been discouraged for 15 years or more in the SQL Server documentation. Microsoft consistently threatens to stop recognizing the syntax. But, apparently they have not followed through on that threat.
The syntax errors you are getting occur because you are mixing the old style joins (comma separated with WHERE clause) with the new style (LEFT OUTER JOIN) with ON clauses.
Your existing query should be changed to something like this. The tables are aliased because it makes it easier to read and is customary. I just made up the JOIN criteria.
SELECT t1_col1, t1_col2, t2_col1, t2_col2, t3_col1
FROM table1 t1
INNER JOIN table2 t2 ON t2.one_ID = t1.one_ID
INNER JOIN table3 t3 ON t3.two_ID = t2.two_ID
LEFT OUTER JOIN table4 t4 ON t4.three_ID = t3.three_ID
I hope that helps with "a push in the right direction."
You may also want to read this post that explains the different ways to join tables in a query. What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
Also for the record the "OLD STYLE" of joining tables (NOT RECOMMENDED) would look like this (but do NOT do this - it is a horrible way to write SQL). And it does not work for left outer joins. Get familiar with using the ...JOIN...ON... syntax:
SELECT t1_col1, t1_col2, t2_col1, t2_col2, t3_col1
FROM table1 t1, table2 t2, table3 t3
LEFT OUTER JOIN table4 t4 ON t4.three_ID = t3.three_ID
WHERE
t2.one_ID = t1.one_ID
AND t3.two_ID = t2.two_ID

Update a column of a table with a column of another table in SQL Server

I am trying to update column from one table to another table of different database.
I searched on stack but found answers for PostgreSQL and SQLite. Those code ain't worked for me properly in SQL Server.
Say,
D1,D2 are the two different databases
T1 is the table
C1,C2,C3 are the columns in T1
I want to update like
UPDATE T1
SET D2.T1.C1 = D1.T1.C1
WHERE D2.T1.C2 = D1.T1.C2
Everything except the where clause works fine.
Here is some code I tried:
use DB2
GO
UPDATE TABLE1
SET COL1 = (SELECT COL1 FROM DB1.dbo.TABLE1)
WHERE COL2 = DB1.dbo.TABLE1.COL2
How shall I write this query?
Note: D1 and D2 are identical. Both have the exact same schema. Just the name and data are different. Both databases are on the same server.
SQL Server supports an update join syntax:
UPDATE t1
SET COL1 = t2.COL1
FROM TABLE1 D1.t1
INNER JOIN TABLE2 D2.t2
ON t1.COL2 = t2.COL2;
Actually, your current approach might work, but you should try changing it to this:
UPDATE TABLE1 D1.t1
SET COL1 = (SELECT t2.COL1 FROM TABLE2 D2.t2 WHERE T1.COL2 = T2.COL2);
You can simply use an INNER JOIN cause sql-server support Cross-Database Queries, so you can do as:
UPDATE T1
SET T1.Col1 = T2.Col1
FROM DataBase1.Schema.TableName T1 INNER JOIN DataBase2.Schema.TableName T2
ON T1.ID = T2.ID;
Please, visit and read Cross-Database Queries.
Or update tableName set column Name = (select columnName from second table where 1=1) i.e condition is either true or false optional though

How to use INSERT INTO with a LEFT JOIN?

I would appreciate if someone could help out.
There are two tables that I need to combine. So far I have been doing this every time.
SELECT * INTO TABLE_3
FROM TABLE_1 LEFT JOIN TABLE_2
ON TABLE_1.[DATE] = TABLE_2.[DATE1]
However, I would like to skip the part of creating a new table and insert the columns I need directly into the existing table.
I tried doing this,
INSERT INTO [TABLE_1] (USD,EUR,RUR)
SELECT USD,EUR,RUR
FROM TABLE_1 AS T1 LEFT JOIN TABLE_2 AS T2
ON T1.[DATE] = T2.[DATE1]
but got an error saying that my column names are ambiguous
I use SQL server 2014.
Instead of giving the column name directly, please specify the alias name to say from which table the column should take. May be here both tables having same column the you are trying to select. You should specify the exact table
INSERT INTO [TABLE_1] (USD,EUR,RUR)
SELECT [T1/T2].USD,[T1/T2].EUR,[T1/T2].RUR
FROM TABLE_1 AS T1 LEFT JOIN TABLE_2 AS T2
ON T1.[DATE] = T2.[DATE1]
Either you can specify T1 or T2 as per your business logic. Please rewrite the query as mentioned here. This will solve the problem. Please try this.

Shortcut for adding table to column name SQL-server 2014

Stupidly simple question, but I just don't know what to google!
If I create a query like this:
Select id, data
from table1
Now I want to join with table2. I can immediately see that the id column is no longer unique and I have to change it to
table1.id
Is there any smart way (like a keyboard-shortcut) to do this, instead of manually adding table1 to every column? Either before I add the Join to secure that all columns will be unique, or after with suggestions based on the different possible tables.
No, there is no helper.
But do not you can alias the table name:
select x.Col1, y.Col2
from ALongTableName x
inner join AReallyReallyLongTableName y on x.Id = y.OtherId
which can also make queries clearer, and is very much necessary when doing self joins.
First of all, you should start using aliases:
SQL aliases are used to give a database table, or a column in a table,
a temporary name.
Basically aliases are created to make column names more readable.
This will narrow down your problem and make your code maintenance easier. If that's not enough, I guess you could start using auto-completion tools, such as these:
SQL Complete
SQL Prompt
ApexSQL Complete
These have your desired functionality, however, they do not always work as expected (at least for me).
Oh! You can use alias table name. Like this:
SELECT A.ID, A.data
FROM TableA A
INNER JOIN TableB B
ON A.ID = B.ID
You just only use A. or B. if two table have same this column selected. If they different, you don't need: Like this:
SELECT A.ID, data -- if Table B not have column data
FROM TableA A
INNER JOIN TableB B
ON A.ID = B.ID
Or:
Select A.*, B.ID
FROM TableA A
INNER JOIN TableB B
ON A.ID = B.ID

How does t-sql update work without a join

I think my head is muddy or something. I'm trying to figure out how a t-sql update works without a join when updating one table from another. I've always used joins in the past but came across a stored proc where someone else created one without a join. This update is being used in SQL 2008R2 and it works.
Update table1
SET col1 = (SELECT TOP 1 colX FROM table2 WHERE colZ = colY),
col2 = (SELECT TOP 1 colE FROM table2 WHERE colZ = colY)
Obviously, colY is a field in table1. To get the same results in a select statement (not update), a join is required. I guess I don't understand how an update works behind the scenes but it must be doing some kind of join?
SQL Server translates those subqueries into joins. You can look at this by getting the query plan. You can write an equivalent query with UPDATE ... FROM ... JOIN syntax and observe the query plan to be essentially the same.
The sample code shown is unusual, hard to understand, redundant and inflexible. I recommend against using this style.
No it's doing a sub query, well two in this case. Be damn painful if you have another 98 col fields.
You can do something similar for select
select *,
(SELECT TOP 1 colX FROM table2 WHERE colZ = colY) as col1
From table1
A left join would simply be more efficient
Your example unless the dbms optimises it it running the subquery(ies) for each row in table.
Got to say whoever wrote it is less than competent.
These subqueries are what is called correlated subqueries. If you were to write the same query as a SELECT rather than an UPDATE it would look like this.
SELECT col1 = (SELECT TOP 1 table2.colX FROM table2 WHERE table2.colZ = table1.colY),
col2 = (SELECT TOP 1 table2.colE FROM table2 WHERE table2.colZ = table1.colY)
FROM table1
The JOIN is in the fact that you are referencing a column from an outside table on the inside of the subquery. Table1 is referenced in the UPDATE command. You can include a FROM clause but it isn't required for a setup like this.
You can use the same syntax in a SELECT with no join, but you need to alias the table if colY also exists in table2
SELECT (SELECT TOP 1 colX FROM table2 WHERE colZ = T.colY)
, (SELECT TOP 1 colE FROM table2 WHERE colZ = T.colY)
FROM table1 AS T
I only ever use this sort of thing when building up an ad hoc query just for my own infomation. If it's going to be put into any sort of permanent code I'll convert it to a join as it's easier to read and more maintainable.

Resources