I'm very confused - I opened up my database in Squirrel SQL to do some SQL and I type this in my SQL entry window:
select * from *
And for whatever reason it gives me this output:
Error: Incorrect syntax near '*'.
SQLState: 42000
ErrorCode: 102
Does anyone know what this means? It's hard to do a google search because Google treats the asterisk as a special character.
I think that second * is invalid, that should be a table name:
SELECT * FROM MyTable
You need to specify the table name.
SELECT * FROM myTable
If however you knew that and you are actually looking for a way to SELECT from all your tables i'm afraid you'll have to type them one by one like this :
SELECT * FROM myTable1
UNION ALL
SELECT * FROM myTable2
UNION ALL
SELECT * FROM myTable3
...
Also remember that if you have to Select like I did on the second example, there is a problem with the way your data is organized in you database. If it's the case, you should take a look at this article on Data Normalization.
Related
Many times a day I have to write similar queries to get single record:
select t.*
from some_table t
where t.Id = 123456
maybe there is some shortcuts for retrieving single record? Like entering id, table and SQL server generates rest code automatically
In Sql Server Go to
Tools-> Options-> Environments->Keyboard
You will get shortcuts, there you can define your own as well as get the standards.
you can set a short cut for a fully executable query like
select * from table where id =20
but not like below
select * from
I have a view setup as
CREATE VIEW dbo.my_data_view
AS
SELECT * from dbo.my_used_data (NOLOCK)
UNION
SELECT * from dbo.my_unused_data (NOLOCK)
go
I currently don't have anything in my_unused_data table. But I expect any query made to work just fine. the following is my query:
select * from my_data_view where code = '5E7230893312001084789839'
The query is failing with the message:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '8E2230893319020101078983' to data type int.
This doesn't make any sense. my_used_data and my_unused_data tables have the identical schema and types so nothing can go wrong there. What I have found out is that when I have data in my_unused_data. It works fine;Otherwise it fails. Is it something special with the views or what?
Problem solved, the order of the columns where swapped for column no. 3 and 4 for my_unused_data that messed things up. I didn't realise that SQL server assumes that ordering of the column is correct when dealing with SELECT * UNION SELECT * queries
I want to be able to create new tables based on any SQL select statement. I have tried the following which I got the format from another question and it does not work (there are similar questions but not one that I found actually works). I keep getting an error on the SQL statement.
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
This is the CREATE TABLE statement:
CREATE TABLE MyNewTable
AS
SELECT *
FROM dbo.Bat
This will copy the entire table including rows
SELECT *
INTO newTableName
FROM dbo.Bat
Add WHERE 1 = 0 to copy just the table structure
If it is SQL Server (the dbo schema, default in SQL Server indicates it is SQL Server), you can do following.
select * into MyNewTable from dbo.Bat;
The SELECT INTO statement does not copy your table constraints.
You statement is a valid Oracle and MySQL statement though.
CREATE TABLE ... AS SELECT is simple (by deliberately ignoring for example the concepts of storage)
To create a table with all its lines
code:
CREATE TABLE XX AS SELECT * FROM YY ;
the result of command in mysql
I have sql query:
select * from table where id in (1,2)
1,2 are parameters which I add there dynamically. But what If I have empty set:
select * from table where id in ()
then this query call exception:
ERROR at line 1:
ORA-00936: missing expression
how should I create sql with empty set
You can always add null to your set, so when the real set is empty, you get no syntax error:
select * from table where id in (null,1,2)
vs
select * from table where id in (null)
Your generator would be simpler, too, because you can print a , in front of every item that you add, without checking if it's the first one or not.
Of course since you are generating your SQL, the standard precautions against SQL Injection apply: do not let user input anywhere near the SQL that you generate.
Try this
select * from table where id in (null)
I am linked to a Proficy Historian that allows periods in the column names. Because the data is stored in a non DBMS format I can not use openquery to get the data because there is no set schema to the tables. So I must use four part name syntax to get the data. This example works:
SELECT * FROM iHist...[SELECT * FROM ihTrend]
but this fails with Incorrect syntax near '.'.
SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07][0].F_CV.Value] FROM ihTrend]
where SERVER.pid_astatus[07][0].F_CV.Value is the name of the column
This fails as well with Incorrect syntax near the keyword 'from'.
SELECT * FROM
iHist...[SELECT [SERVER.pid_astatus[[07]][[0]].F_CV.Value] from ihTrend]`
Any ideas on how I can make SQL Server see this as a column?
EDIT:
Martins suggestion of the right brackets to escape the brackets work only on the outside of the sql call
SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM iHist...[SELECT * FROM ihTrend]
However it does not work inside Incorrect syntax near the keyword 'from'.
SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM ihTrend]
EDIT
SELECT * FROM iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value]] FROM ihTrend]
I had to escape the column escape :)
You only need to escape these ]
[pid_astatus[07]][0]].F_CV.Value]
This works for me
CREATE TABLE #t(
[pid_astatus[07]][0]].F_CV.Value] int
)
SELECT [pid_astatus[07]][0]].F_CV.Value]
FROM #t
(Edited to reflect new knowledge, if you like this vote for Martin Smith's answer instead!)
Escape the ] by doubling them:
SELECT * FROM
iHist...[SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] from ihTrend]
Based on your comment, try:
SELECT [SERVER.pid_astatus[07]][0]].F_CV.Value] FROM iHist...ihTrend