I've been running a query on a large number of servers, and on just three of them (SQL2000, SQL2005 AND SQL2008) the query fails. I've boiled it down to referencing a table in another DB.
SELECT * FROM MASTER.dbo.syslogins AS syslogins
Which works on 99% of the servers, but on the three gives:
Invalid object name 'MASTER.dbo.syslogins'
If I switch to the master DB and run
SELECT * FROM dbo.syslogins AS syslogins
Then it works. I've also checked I have permissions by running
SELECT IS_SRVROLEMEMBER('sysadmin');
Which returns a 1.
So why does referencing the MASTER.dbo.syslogins table fail?
Maybe the server is set up with a case sensitive collation. Try
SELECT * FROM master.dbo.syslogins
Related
I need to copy tables from a linked server onto my local machine. I am working in SQL management studio. The linked server is Oracle based. My end goal is to set up a stored proc that deletes a table if it exists and creates a new table in its place with refreshed data. This will be done for many tables as needed. The issue with the below code is that I get the error:
Incorrect syntax near the keyword 'SELECT'.
I am stuck at creating the table.
CREATE TABLE test AS
SELECT DUMMY
FROM OPENQUERY (LServer, '
Select *
from sourceT
');
The data in the dummy table is just one column with a single value "x". I have seen posts that suggest using a certain notation in naming the linked server table, like <server.database.schema.tablename> but this doesn't seem to work,even if I just run the select statement using the openquery. If I just run the select part in the script above, this does work.
CREATE TABLE test AS
Is valid in Oracle but not SQL Server
You want
-- if the table already exists drop it
DROP TABLE IF EXISTS test;
-- now create a table and load into it
SELECT DUMMY
INTO test
FROM OPENQUERY (LServer, '
Select *
from sourceT')
How do I drop a few tables (e.g. 1 - 3) using the output of a SELECT statement for the table names? This is probably standard SQL, but specifically I'm using Apache Impala SQL accessed via Apache Zeppelin.
So I have a table called tables_to_drop with a single column called "table_name". This will have one to a few entries in it, each with the name of another temporary table that was generated as the result of other processes. As part of my cleanup I need to drop these temporary tables whose names are listed in the "tables_to_drop" table.
Conceptually I was thinking of an SQL command like:
DROP TABLE (SELECT table_name FROM tables_to_drop);
or:
WITH subquery1 AS (SELECT table_name FROM tables_to_drop) DROP TABLE * FROM subquery1;
Neither of these work (syntax errors). Any ideas please?
even in standard sql this is not possible to do it the way you showed.
in standard sql usually you can use dynamic sql which impala doesn't support.
however you can write an impala script and run it in impala shell but it's going to be complicated for such task, I would prepare the drop statement using select and run it manually if this is one-time thing:
select concat('DROP TABLE IF EXISTS ',table_name) dropstatements
from tables_to_drop
I have a SQL Server in Spain and one in the US and there is a domain trust between the two with linked servers on each for access to the other.
I would like to be able to run the below query on the US SQL Server without having to maintain a stored proc on the US Server in order to run it. Is there a way to create a passthrough query from the SQL Server in Spain? I've already tried using OPENQUERY and OPENROWSET and it's just not working as they only seem to work with select statements that return results:
DROP TABLE IF EXISTS [Global].[dbo].[WW_Customer_Receivables]
SELECT *
INTO [global].[dbo].[ww_customer_receivables]
FROM
[LinkedServerObject-Spain].[global].dbo.ww_customer_receivables
If you want to execute DDL statement on your linked server with openquery you can with the following trick:
SELECT *
FROM OPENQUERY(linkedserver, '
DROP TABLE IF EXISTS [Global].[dbo].[WW_Customer_Receivables]
SELECT ##RowCount')
The SELECT ##RowCount returns a result set. So OPENQUERY works.
This trick works for all DDL operations like create, alter, drop. Or if you want to perform inserts/updates/deletes that don't return a result set.
Same trick is applied here Where they have a dummy select foobar.
If you want to execute the into statement from the openquery you can do it like this:
DROP TABLE IF EXISTS [Global].[dbo].[WW_Customer_Receivables]
SELECT *
INTO [global].[dbo].[ww_customer_receivables]
FROM OPENQUERY([LinkerServerObject-US], '
SELECT *
FROM [global].dbo.ww_customer_receivables')
I am trying to compare the results of a linked server query with a local query. What I am try to do is find out what Logins exist on one server but not the other. Just windows auth accounts is fine for now.
My current query is
Select name
from [linkedServer].master.[sys].[server_principals]
Where name not in ('Select name from sys.server_principals')
What I get back is the result of the linked server query only with the where ignored. How do I go about comparing the results of the two?
First, use not exists. Second, I think your single quotes are wrong:
select sp.name
from [linkedServer].master.[sys].[server_principals] sp
where not exists (select 1
from sys.server_principals sp2
where sp2.name = sp.name
);
I strongly recommend that you get in the habit of using not exists with a subquery rather than not in, because not in returns no row if any value from the subquery is NULL. That is usually not desirable behavior.
I have a server called GreatPlains and I would like to create a new table (not already defined) using the INSERT INTO clause onto my local server's reporting database. We have a linked server set up for the GreatPlains server and our main production server.
Simplified version of current query:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders]
I'm also getting the error:
The object name 'local.Reporting.dbo.NewTable' contains more than the
maximum number of prefixes. The maximum is 2.
There are two mistakes in your query
1.INTO clause support maximum of 2 prefixes. You cannot include SERVER NAME
DATABASE_NAME.SCHEMA_NAME.TABLE_NAME
2.Unwanted INSERT ketword
So your query should be
SELECT *
INTO [Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
I think you have an extra insert:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
If the table is already defined and has the same columns in the same order, then you can do:
INSERT INTO [local].[Reporting].[dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders];
If you are running the script in your local server and table already exists in the database,Use the below script.
USE [Reporting]
GO
INSERT INTO [dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders]
If you don't have the table in your database,use the below script.
USE [Reporting]
GO
SELECT *
INTO dbo.[NewTable]
FROM [linked].[Main].[dbo].[Orders]