Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL syntax with a restricted word

Posted on 2013-01-01
6
Medium Priority
?
274 Views
Last Modified: 2013-01-02
I needto execute a query to copy a complete table from one database (not on this SQL server) into a new database using a linked server. The complication seems to be that the table is named "FUNCTION"


--Select * into "FUNCTION from openquery(NEW,' select * from FUNCTION') this statement fails

I cannot rename the source table
0
Comment
Question by:hwassinger
6 Comments
 
LVL 17

Expert Comment

by:Kent Dyer
ID: 38735203
SELECT INTO is reserved as you are trying to SELECT * INTO NEW_TABLE FROM OLD_TABLE

That is probably the best way to think of this.

HTH,

Kent
0
 

Author Comment

by:hwassinger
ID: 38735206
Kent

thats not going to be it as I have MANY other queries that seem to be working with this syntax.... It seems tio be related to the FUNCTION as the table name.
0
 
LVL 23

Accepted Solution

by:
Steve Wales earned 2000 total points
ID: 38735208
You can usually get around reserved words be enclosing them in square brackets like so: [function]

Not sure if that will work inside openquery though.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 66

Expert Comment

by:Jim Horn
ID: 38735327
Guessing the above square bracket solution would work.

I understand you can't rename the source table, but a possible workaround if the above doesn't work would be to create a view that does a SELECT * FROM FUNCTION, and save it as a name that isn't a reserved word.

>I have MANY other queries that seem to be working with this syntax
Hmm... if this is correct, and square brackets don't work, you might be somewhat hosed..
0
 
LVL 8

Expert Comment

by:jpgobert
ID: 38735406
So... I'm curious why you wouldn't fully qualify the table names since you're working with two tables in the same query with the same name, let alone the fact that it is a reserved word... brackets will fix SQL thinking your table name is an actual reserved command word...

I know you have a linked server connection in place but I'd definitely write it out as

[SERVER].[DATABASE].DBO.[FUNCTION]

What's the actual error that SQL throws at you on this?
0
 

Author Closing Comment

by:hwassinger
ID: 38736291
thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question