I have numerous databases in compatibility_level of 80 (SQL Server 2000);
I need to execute the following:
select
sf.fileid, sf.groupid, sf.name, sf.filename, mf.database_id
from
sys.sysfiles sf
JOIN
sys.master_files mf ON sf.filename = mf.physical_name COLLATE DATABASE_DEFAULT
CROSS APPLY
sys.dm_os_volume_stats (mf.database_id, sf.fileid)
But I get the following error:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '.'.
I cannot change the compatibility mode and I have no idea how I can run this specific script for my database from the context of master db.
In SQL Server 2000 there were no 'sys' files. Many had equivalents and you can see a good chart at:
http://www.mssqltips.com/sqlservertip/1037/system-information-in-sql-server-2000-vs-sql-server-2005/
In 2000 for example there was a dbo.sysfiles but no sys.sysfiles. These tables do not necessarily contain the same columns.....
"CROSS APPLY" is not allowed in earlier versions of the SQL as well in databases running in older compatibility models.
Related
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"" ')
So, kind of an SQL Newbie here and I am trying to get something to work on Microsoft SQL Server 2005 (yay for old outdated databases powering businesses still).
I can get it to work properly on my local dev machine (running SQL Server 2019) but when I run it on the 2005 server it errors out.
Query:
MERGE CustomDB.[dbo].StockCounts AS [Target]
USING (SELECT ID,
ProductNo
FROM CompanyDBReplication.[dbo].STOCKPRODUCT) AS [Source] (ID,
ProductNo)
ON [Target].ID = [Source].ID
WHEN NOT MATCHED THEN
INSERT (id,
ProductNo,
CountDate,
CountID)
VALUES ([Source].ID,
[Source].ProductNo,
NULL,
NULL);
Error:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '.'.
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'AS'.
Now I don't know enough about the differences here of why this would error out nor how I would go about searching this (I really don't do SQL ever and sort of had to Google this to make it work in the first place).
Basically I want to copy/merge items from a source database into the target database and add new ones that might get added to the source if they are not found in the target.
If someone can help me either fix this one to work on SQL Server 2005 or propose/give me an example of a different solution that will accomplish the same thing and work on SQL Server 2005 that would be awesome and I would forever be indebted.
The source of where I got this solution is here: https://stackoverflow.com/a/34892729/5877943
Merge statement appeared only in MSSQL 2008, you can use an outer join instead.
Something like this.
INSERT INTO CustomDB.[dbo].StockCounts (
id,
ProductNo,
CountDate,
CountID
)
SELECT
[Source].ID,
[Source].ProductNo,
NULL,
NULL
FROM CompanyDBReplication.[dbo].STOCKPRODUCT) AS [Source]
LEFT JOIN CustomDB.[dbo].StockCounts AS [Target] ON [Target].ID = [Source].ID
WHERE [TARGET].Id IS NULL;
I read in SQL Server documentation that I can specify column names in FROM clause of a simple SELECT query.
But when I try to run this query:
select * from my_db.dbo.test_table.test;
I get the following error:
Msg 7202, Level 11, State 2, Line 1 Could not find server 'my_db' in
sys.servers. Verify that the correct server name was specified. If
necessary, execute the stored procedure sp_addlinkedserver to add the
server to sys.servers.
I know this is a T-SQL page, and I'm trying to run .
Why does this happen?
I'm using SQL Server version 2017 via Microsoft SQL Server Management Studio version 2017 as well.
try rewriting from this
select * from my_db.dbo.test_table.test;
to this
select test_table.test,* from my_db.dbo.test_table;
the way it was written with that many Periods it assume you are trying to fully qualify the table so in what you had tried to the server is treating it as follows
my db = linked server (a server other than the Server you are working on)
dbo = Schema (which is correct)
test_table = Table (Also correct)
test = just plain erroror
the fields you want to show should directly follow the Keyword Select so if you only wanted 2 fields you could write
select test,test2 from my_db.dbo.test_table;
or simpler if you only have the one server
select test,test2 from dbo.test_table;
or if you only have the defailt Schema dbo (Database Base Owner)
select test,test2 from test_table;
I hope thaelps
You cannot specify columns in the FROM clause, but you can specify columns in the SELECT clause.
select test from my_db.dbo.test_table
Please run sp_addlinkedserver and sp_addlinkedsrvlogin SPs:
EXEC sp_addlinkedserver #server='MyLinkedServer'
EXEC sp_addlinkedsrvlogin 'MyLinkedServer', 'false', NULL, 'MyUserName', 'MyPassword'
After that, you can try to run: select * from my_db.dbo.test_table
Ref: https://blog.sqlauthority.com/2014/08/26/sql-server-fix-error-7202-could-not-find-server-in-sys-servers-verify-that-the-correct-server-name-was-specified/
Also, even though it is SQL Server 2014 related maybe helps, please see: Could not find server 'server name' in sys.servers. SQL Server 2014
I am working on 2 versions of SQL Server i.e 2005 and 2008 R2.
From 2008 R2 I have created a linked server which will connect to an older SQL Server 2005 instance.
I have one table on my server (2008) which is as below
members
id name
0002320 AOne Enterprises Motihari
0002321 AOne Enterprises Siliguri
Another table which resides on remote server contain activity of each agent
id member_code agent rr_no txn_date amount
I fired a query below
select top 5 *
from [192.168.6.3].sync.dbo.agents_log
where member_code IN
(select id from members where name like 'AOne Enterprises%')
I was trying to pull all activity log of AOne Enterprises through out the country which is in distributed database , so I need to create a link servers.
I got this error:
Msg 468, Level 16, State 9, Line 1
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_BIN" in the equal to operation.
not exactly sure what you need, but if its only collation issue you can do below
SELECT TOP 5 *
FROM [192.168.6.3].sync.dbo.agents_log
WHERE member_code COLLATE SQL_Latin1_General_CP1_CI_AS
IN (SELECT id
FROM members
WHERE NAME LIKE 'AOne Enterprises%')
I just added COLLATE SQL_Latin1_General_CP1_CI_AS , perhaps it work
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