column names with periods on a 2008 linked server - sql

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

Related

Understanding of difference between MariaDB and SQL Server for a query

I am using MS SQL Server Version 2008 and MariaDB 10.5.
My table and data :
Following is the query i am running on both the databases.
select distinct distributor from market order by city
Found this query is failing in MS SQL Server database with error
SQL Error [145] [S0001]: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Understood the reason why it failing
Reason for - ORDER BY items must appear in the select list if SELECT DISTINCT is specified
when i ran the same query run with MariaDB it ran successful without any error and gave me below output.
So i have doubt why MariaDB is behave different here? ideally it should fail right?
Thanks in advance.
Please try this query
Select distinct col from (
Select col from table order by col
)

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"" ')

Can't insert multiple rows in SQL Server Manager Express 2005

I have a Table HORAS_X with ID_HORA(int), ID_ZONA(int), DESCRIPCION(nvarchar), COMIDA(bit), META(int), NUMERO(int).
I can insert a single row like:
INSERT INTO HORA_X
(ID_HORA,ID_ZONA,DESCRIPCION,COMIDA,META,NUMERO)
VALUES
(2,2,'06:00-07:00',0,174,1);
And it works.
However when I try to insert multiple rows like this:
INSERT INTO HORA_X
(ID_HORA,ID_ZONA,DESCRIPCION,COMIDA,META,NUMERO)
VALUES
(3,3,'06:00-07:00',0,174,1),
(4,4,'06:00-07:00',0,174,1);
It throws the error
Msg 102, Level 15, State 1, Line 2 Wrong syntax near ','.`
Is my Syntax wrong? I checked online and it's supposed to be fine.
And yes, I have restarted SQL Server Manager, thanks for any lead and help.
If I remember correctly, SQL Server 2005 does not support VALUES table value constructor. It was introduced in SQL Server 2008, so for SQL Server 2005 you need to use the following statement:
INSERT INTO HORA_X (ID_HORA,ID_ZONA,DESCRIPCION,COMIDA,META,NUMERO) VALUES (3,3,'06:00-07:00',0,174,1)
INSERT INTO HORA_X (ID_HORA,ID_ZONA,DESCRIPCION,COMIDA,META,NUMERO) VALUES (4,4,'06:00-07:00',0,174,1);
Notes: SQL Server Manager Express 2005 is a tool, but I assume, that you are using SQL Server 2005.

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

Can a fake table be created in SQL Server 2005

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;