Can a fake table be created in SQL Server 2005 - sql

I would like to know if it is possible to create a fake table in SQL Server 2005.
Because when I tried I got an error
Msg 208, Level 16, State 1, Line 1
Invalid object name 'dual'.
This is what I tried
and it did not work. For test purpose I have created a test table to execute the query.

In SQL Server you are allowed to leave out the FROM clause. So you don't need a fake table.
Instead of writing
select 42
from dual;
just write
select 42;

Related

Not getting SQL values from Linked Server (Oracle Database)

I have an Oracle Database, Which is linked to an Microsoft SQL Server, so I can query the server and spool information from the database without worries directly from SQL Server Management Studio. Now I want to pass a SQL query to fetch information from the linked server and display the values from the database.
So when I do this for instance
select * from openquery(LinkServerName,'select * from table_name')
This works 100%, no errors nothing.
Now when I run something like this
select * from openquery(LinkServerName,'select foracid,acct_name,acct_crncy_code,clr_bal_amt from table_name where bacid='1010000001' and sol_id='XXX'')
Where bacid and sol_id are both strings as columns, I get this return error:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '1010000001'.
Please what do I appear to be missing?
select *
from openquery(LinkServerName,'select foracid,acct_name,acct_crncy_code,clr_bal_amt from table_name where bacid=''1010000001'' and sol_id=''XXX'' ')
should work, ' needs to be escaped by '' (two ').
Correction based on comment (escaping is somewhat strange in this case):
select *
from openquery(LinkServerName,'select
foracid,acct_name,acct_crncy_code,clr_bal_amt from table_name where
bacid=""1010000001"" and sol_id=""XXX"" ')

insert records in sql from local server to linked server that does not exist in linked server

SELECT * from TABLE_attendance
WHERE date NOT IN
(SELECT * from [LINKED SERVER].DATABASENAME.dbo.TABLE_attendance where date = '06-09-15')
When I executed this query I got this error message:
Msg 116, Level 16,
State 1, Line 3 Only one expression can be specified in the select
list when the subquery is not introduced with EXISTS.
I just wanted to check a records in linked server if the records are all existing from local server , if the linked server has no record on an specific date, then the local server will transfer data into linked server.
Please help me to solve this problem, Thank you :-)
You cannot return more than one column in your subquery. Your query should be something like this
SELECT * from TABLE_attendance
WHERE id NOT IN (SELECT id
from [LINKED SERVER].DATABASENAME.dbo.TABLE_attendance
where date = '06-09-15')

I want to create a new table from a SQL select statement?

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

column names with periods on a 2008 linked server

I'm using SQL Server 2005 and trying to select columns from a SQL Server 2008 linked server that have periods in them.
I've checked this post:
Selecting a column with period in the column name SQL Server
But I'm not getting the same error.
This is the code
INSERT INTO [Linked_Server].Database.dbo.Table
([Column_Name], [Column.Name])
SELECT
[Column_Name], [Column.Name] FROM local_table
I can select from the table locally, but when I try to select the same columns from the same table on the linked server, I get this error:
Msg 207, Level 16, State 1, Line 3
Invalid column name 'Column.Name'.
EDIT: Fixed a typo with "[Column_Name)". This was a typo only in stackoverflow however.
The typo is not the problem
Replace ) on ] after Column_Name and try once again
INSERT INTO [Linked_Server].Database.dbo.Table
([Column_Name], [Column.Name])
SELECT [Column_Name], [Column.Name] FROM local_table
Looks like this is a known issue with Linked Servers in SQL Server 2008. I had seen this page while I was researching, but I didn't see the workaround section at the bottom.
I ended up changing the query to use OPENQUERY instead, and now it works.
http://support.microsoft.com/kb/972856

Merge statement error in SQL Server 2008

I am executing the following merge statement in SQL Server 2008:
MERGE
PopulationData AS a
USING ImagesData AS b
ON a.ID = b.ID
WHEN MATCHED THEN
UPDATE SET a.SURNAME = 'joe123'
WHEN NOT MATCHED THEN INSERT(a.ID,a.SURNAME)
VALUES (12454,'joe123');
I have the following error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Can anyone tell me where the syntax error.
Parsing your query in SQL Management Studio gives me the following error:
Msg 10739, Level 15, State 1, Line 7 The insert column list used in
the MERGE statement cannot contain multi-part identifiers. Use single
part identifiers instead.
I then remove the identifiers...
MERGE
PopulationData AS a
USING ImagesData AS b
ON a.ID = b.ID
WHEN MATCHED THEN
UPDATE SET a.SURNAME = 'joe123'
WHEN NOT MATCHED THEN INSERT(ID,SURNAME)
VALUES (12454,'joe123');
...and the query parses successfully. Therefore, the syntax error is almost certainly not coming from your MERGE statement. Are you really executing only the statement you posted, or is it part of a larger script or procedure? And if you double-click the error message, it should highlight the line where the syntax error is (at least with SQL 2008).
Update: I noticed that you have tagged the question for SQL 2005 and 2008, but MERGE is only supported in SQL 2008. Parsing the query under SQL 2005 gives the syntax error.
the problem that i was executing the query on sql server management studio 2008 but i was connecting to sql server 2005 database on another server. Now it is fixed