Create View using Linked Server db in SQL Server - sql

How can I create View on Linked Server db. For Example I have a linked server [1.2.3.4] on [5.6.7.8]. Both db servers are SQL Sserver 2005. I want to create View on [5.6.7.8] using table on linked server.
EDIT:
On creating using full name, [1.2.3.4].db.dbo.table, I am getting this error.
SQL Execution Error.
Executed SQL statement: SELECT * FROM 1.2.3.4.db.dbo.table (YOu can see brackets are not there.)
Error Source: .Net SqlClient Data Provider
Error Message: Incorrect syntax near '0.0'. ---> part of IP address.
I am just creating this in ManagementStudio, not using it because it is not created yet. I Have changed IP. In image you can see there are not brackets around IP but I given it and on error these brackets are removed.
Thanks.

You need to use the four part qualified name: linkedserver.database.schema.table
SELECT * FROM [1.2.3.4].Northwind.dbo.Customers
Here is an MSDN article about accessing object names.
You might want to try manually creating the view, rather than using the SQL Management tools:
CREATE VIEW [dbo].[sywx]
AS
SELECT *
FROM [1.2.3.4].Atia.dbo.IpPbxDCR
GO
I also recommend that you use a name, if possible, for the linked server rather than using the IP address.

Its a SQL Management Studio Issue.
If you try to create the view using management studio NEW VIEW then you get that error incorrect syntax.
But if you use SQL Query:
CREATE VIEW [dbo].[viewname]
AS
SELECT *
FROM [0.0.0.0].database.dbo.table
GO
then it will work.
To test you will see the view created when you refresh views.
If you just do a select query from the view you will see the view return results.
But if you try to go into design mode for that view and try executing the design query the error will pop up again even though the view was successfully created.

If the linked server is set up, you just reference tables on it using a four-part qualified name:
linkedserver.database.schema.table
So if your linked server name is [0.0.0.0], you can reference a table as:
[0.0.0.0].database.schema.table

your main problem is naming your Link Server with numbers (IP address). The only way that worked for me was using an alphabetical name for my Link Server without any dot '.'
You can use these lines of code to add your link server and authentication:
EXEC sp_addlinkedserver
#server='TEST_LINK',
#srvproduct='',
#provider='SQLNCLI',
#datasrc='tcp:0.0.0.0'
EXEC sp_addlinkedsrvlogin
#useself='FALSE',
#rmtsrvname='TEST_LINK',
#rmtuser='user',
#rmtpassword='secret'
You can find the original answer here

For linked servers using the periods for a web service name - this won't work.
The code details and steps of the difference is posted at:
http://www.access-programmers.co.uk/forums/showthread.php?t=260764
e.g. FROM [V2.EGG.COM]..[NAT_DBA].[NV_WELLS]
In SSMS 2008, the square brackets around the [V2.EGG.COM].. are removed by the editor. Then the update failes because it is more than 4 parts.
Have searched in vain to find a work-around.
The Script that does work is posted at the link above.

You can also just drag and drop the table/view object from the linked server into your view and Management Studio will create the four part reference for you.

You need to define a Linked Server before you can access it, since the linked server entry also contains the authentication details.
After creating a linked server, you can access its databases and their objects using the dot notation servername.database.[owner].object

Related

Where are SQL Linked Server view definitions?

I've migrated and moved linked servers before, but never had to modify one, so this issue is mostly foreign to me.
Here's the situation (with table names obscured but consistent):
SQL Linked Server SQLXYZ1-> Catalog XYZ -> View HRIS.ZZ_VIEWNAME
This "view" definition maps to a DB2 table on the AS/400 called DB2.TABLENAME.
Here's the issue: I added a new column to the DB2 table DB2.TABLENAME, but when it comes through the linked server (via this view), the new column is coming through wrong because the linked server side of the view definition doesn't know about it.
If I right-click and generate a Select statement from view HRIS.ZZ_VIEWNAME on the SQL linked server side, the columns that come back is the OLD definition of the table that no longer matches DB2.TABLENAME. There's also the fact that the linked server side of things has a prefix of "ZZ_" on the table name, which someone had to type in somewhere, but I can't figure out where... So if ZZ_VIEWNAME is a "view" that the linked server sees, how can I find out where this view is? Is it a SQL view or DB2 view?
My question is: Where is the "definition" of the view that the SQL linked server sees?
I've asked our top SQL manager here, who insisted it's not in SQL, but must exist on the AS/400.
I've asked our top AS/400 manager here, who insisted that such a thing is not done on the AS/400/DB2 and must exist on the SQL side.
So essentially, nobody knows where this definition is.
As someone who doesn't know linked servers and the experts here can't agree, where is the definition of external view that a SQL linked server sees?
Thanks!
Turns out there was another dev's intermediary linked server in the middle, so my question was invalid from the very start.
Someone overarchitected a disaster of a database which is apparently going:
Application -> SQL server view -> Linked Server -> SQL server view -> Synonym in an incorrect database -> Linked Server -> DB2 table

sql server giving error : is not a recognized function name

I created a backup of a database on sql server 200. I created a new database in sql server 2008 r2.
Now when i run a view i get the error :
'function_name' is not a recognized function name.
The function is there
And i can run it using
SELECT [dbo].[function_name] (
'hjh')
GO
SELECT dbo.function_name('kjk')
Why would this problem occur when it is functioning correctly originally?
EDIT:
I think it may be a security issue as schemas owned by the user under dbo does not contain antyhing?
Make sure you are executing it in the correct database context.
If the view is in Database2 and the function is in Database1 then you will need to fully qualify the function using the three part name:
Database1.dbo.[Function_Name]
All objects in a view are assumed to be in the same database as the view unless you specify otherwise.
Is the view on the same database as the function?
If they are not, you need to call it like [database_name].dbo.[function_name]

SQL Server 2005 Linked server not finding tables

I have a linked server where I can clearly see all the databases and tables, so I know the server is properly linked. However, when I try to execute a query, it says invalid object name, at the linked server's table.
The linked server is aliased as TCS, therefore, my query takes that table as
FROM [TCS].dbo.table as b
I have also tried including the database name also as FROM [TCS\db1].dbo.table.
What am I missing here?
Try including the DB name like so:
FROM [TCS].db1.dbo.table as b
I don't think you can specify the DB using a slash.
I would also check to make sure your security settings for the linked server are allowing your account to connect. This article touches on how to do that.
either:
the user (used for the link) doesn't have access to the table; Grant access;
the default DB on the server doesn't have the table. You have to change it to the relevant one or included in the db in the name: [TCS].DATABASE.dbo.table as b;

How do i get the names of all the tables inside a database?

EDIT2: Found a fix! I used the number of the desired schema instead of the name. Should've thought of that before, really! And i think the error messages could've been a bit better aswell. Thanks for all your time!
How can i get the names of all tables inside a database through sql inside asp classic?
The server is running windows 2008, iis7.5 and microsoft jet. I've tried all the querys i could find on the internet (and here) but none have worked.
If i add a ; to the query to run a set of querys at the same time it gives me an error because the statement isn't over at the semicolon.
The master.mdf database cannot be accessed because it's of unknown format.
The sysobjects variable apparently doesn't exist.
I am using mssql 2000 format. (.mdf)
The connection is made through classic asp with the Microsoft.Jet.OLEDB.4.0 provider and ADODB connection/recordset.
How do I get list of all tables in a database using TSQL?
Query to get the names of all tables in SQL Server 2008 Database
EDIT:
I've found two folders containing databases. One is in C:\program files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\mssql\binn\templates and contains master.mdf, mastlog.ldf, model.mdf, modellog.ldf, msdbdata.mdf and msdblog.ldf. The other one is also in the \binn\template data directory and contains master.mdf, mastlog.ldf, model.mdf, modellog.ldf, MSDBData.mdf, MSDBLog.ldf, mssqlsystemresource.ldf, mssqlsystemresource.mdf, tempdb.mdf and templog.ldf. Maybe these is of some interest?
How can i tell if i have permission? Does it give a permission denied error?
Please help! No, don't. Read the 2nd edit at the top.
USE YOUR_DATABASE
GO
SELECT *
FROM sys.Tables
GO
Have you tried the example from:
http://www.kamath.com/codelibrary/cl002_listtables.asp
I almost always use the INFORMATION_SCHEMA views:
SELECT * FROM INFORMATION_SCHEMA.TABLES
If this isn't working for you, the SQL user your site is running under may not have access to the system objects. This is actually a good thing, as giving your site access to the underlying database schema can leave you vulnerable to SQL injection.
So if you do go this route, proceed with caution.
The mdf by itself is useless: you need a database engine (a.k.a. a SQL Server instance) to "run" it. As I understand the question, this is your problem.
Then you can use sysobjects in your database: unless you have added your tables to the master database
There is no practical way to use an mdf directly: if nothing else download MSDE

SSIS and MySQL - Table Name Delimiter Issue

I am trying to insert rows into a MySQL database from an Access database using SQL Server 2008 SSIS.
TITLE: Microsoft SQL Server Management Studio
------------------------------
ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.51a-community-nt]You have
an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near '"orders"' at line 1
The problem is with the delimiters. I am using the 5.1 ODBC driver, and I can connect to MySql and select a table from the ADO.Net destination data source.
The MySql tables all show up delimited with double-quotes in the SSIS package editor:
"shipto addresses"
Removing the double quotes from the "Use a table or view" text box on the ADO.NET Destination Editor or replacing them with something else does not work if there is a space in the table name.
When SSIS puts the Insert query together, it retains the double quotes and adds single quotes.
The error above is shown when I click on "Preview" in the editor, and a similar error is thrown when I run the package (albeit then from the actual insert statement).
I don't seem to have control over this behavior. Any suggestions? Other package types where I can hand-code the SQL don't have this problem.
Sorry InnerJoin, I had to take the accepted answer away from you. I found a workaround here:
The solution is to reuse the connection for all tasks, and to turn ANSI quotes on for the connection before you do any inserts, with an Execute Sql task that runs the following:
set sql_mode='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,
NO_ENGINE_SUBSTITUTION,ANSI_QUOTES'
Try using square brackets around the table names. That may help.
EDIT: If you can, I would create views (with no spaces) based on the Access tables, and use those to export. Even if it means building another Access database with linked tables, I think this is your best bet.
I've always struggled with using SSIS with MYSQL directly. Even after installing the ODBC drivers, they just don't play well in data flows. I've always ended up creating linked ODBC connections between SQL Server and MYSQL. I then rely on linked server queries to bring over data. Instead of using a SSIS data flow task, I use an Execute SQL command, usually in the form of a stored procedure that executes an OPENQUERY.
One solution you could do is load the data into a SQL Server database and use it as a staging environment before you load it into the MYSQL database. I regularly move data between SQL Server 2008 and MYSQL and in the past I use to regularly move data between Access and SQL Server.
Another possible solution is to transform the incoming Access data before it loads into the MYSQL database. That may give you a chance to clean up the column names and the actual data that's going through to MYSQL.
Let me know if either of these work for you.
You can locate the configuration setting file my.ini at <<Drive>>:\ProgramData\MySQL\MySQL Server 5.6\my.ini and add "ANSI_QUOTES" to sql-mode.
e.g: sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ANSI_QUOTES". This should solve the issue while previewing in the SSIS editor.