1

I want to copy data from SQL Server A to SQL Server B; so I have query statement like:

INSERT INTO ServerA_Name.db_name.table_name 
SELECT * FROM ServerB_Name.db_name.table_name where date >= GETDATE()

However the ServerA' name has a illegal character: '-' (ex: A-01) and I can't change the ServerA' name since it would cause other potential problem. So I got the: Incorrect syntax near '-'. error.... Is there a good way to solve this issue?

4
  • 2
    Can you not put the table names in double quotes? Commented Jan 17, 2018 at 0:46
  • Put the table or column name is square brackets. But I don't see dash being used anywhere in your query. Commented Jan 17, 2018 at 0:48
  • 1
    hi jones321, you may want to read this answer first, it may be a good reference. Commented Jan 17, 2018 at 0:48
  • 1
    Possible duplicate of can I give SQL Server database name with hyphen like abc-123? Commented Jan 17, 2018 at 0:50

1 Answer 1

2

If Server A's name contained an illegal character (which I don't see one here in your case) you would need to place that server's name into square brackets, such as:

INSERT INTO [ServerA_Name].db_name.table_name

Square brackets in SQL server act as quotes and allow you to use special (or illegal) characters or spaces.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.