Invalid column name when running a query in SQL Server 2008 - sql-server

I am trying to run a query in SQL Server 2008. It looks like this:
IF EXISTS (SELECT name FROM sysobjects WHERE name = "Bonds" AND type = 'U')
DROP table Bonds
GO
When I run this, I get this error:
Msg 207, Level 16, State 1, Line 2
Invalid column name 'Bonds'.
Msg 28102, Level 16, State 1, Line 3
This query was created by SQL Server. I am trying to run it in a different computer. Then I face this issue.
I have tried Ctrl+Shift+R as this post: SQL Server Invalid Column name after adding new column. But it is not helping.
Need some guidance on this.

Change
WHERE name = "Bonds"
to
WHERE name = 'Bonds'
Otherwise "Bonds" is treated like a column-name which does not exist.

use single quotes in search condition
WHERE name = 'Bonds'

I think you can also use
SET QUOTED_IDENTIFIER OFF;
before your query.

Related

Can create a table but doesn't display in Object explorer, can't select, or delete table?

Using SQL Server 2016. Have a locally hosted database that uses the Windows login for the sa, which is what I am using to login.
Yesterday I ran
CREATE TABLE [Otis].[AnalyzerGroups]
(
[id] [int] IDENTITY,
[Name] [varchar](50) NULL
);
and got command successfully completed. Today I tried selected from this table but got an error:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'Otis.AnalyzerGroups'
So I thought I misremembered and tried running the create statement again but then got the error -
Msg 15530, Level 16, State 1, Line 1
The object with name "AnalyzerGroups" already exists.
The statement has been terminated.
So then I tried DROP TABLE [Otis].[AnalyzerGroups]; and got
Msg 3701, Level 11, State 5, Line 1
Cannot drop the table 'Otis.AnalyzerGroups', because it does not exist or you do not have permission.
I tried making a new test table and the same thing. The first time I run the create statement command successfully completes, but then I can't select / insert / drop from the table, and I cannot see it in the Object explorer either.
I assume this must be some permissions issue but I don't know what property is keeping me from viewing these tables - its not like I'm putting security on these tables, and I can see every other table in our database. Any thoughts?
You put it most likely in the wrong database. Try this.
sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like
''%AnalyzerGroups%'''
There was a trigger in the SQL Server database that fired any time a new table was created under any schema, and would then transfer it to the dbo schema. So my tables did exist, but they were under the schema I was expecting because of this trigger. Why does this trigger exist? Got me. But it's in production and has been for over a decade so there's some reason/it's definitely not changing. Thanks for the help though everyone.

SQL DML - unable to use the after function in modify

I need to update around 1000 xml records with a new xml node, the xml's are stored in a sql table column, with xml datatype, So I've been trying to utilize the modify function documented HERE (specifically example A.4)
However, When I run this against my staging environment (SqlServer 2016 SP2-CU13) I receive the following error message:
Msg 2395, Level 16, State 1, Line 7
XQuery [production.dbo.organisations.Configuration.modify()]: There is no function '{http://www.w3.org/2004/07/xpath-functions}:AFTER()'
The code is rudimentary, I'm just stumped by the error message. Any help or insight is greatly appreciated
Code:
update [production].[dbo].[organisations] Set configuration.modify('INSERT <LegalEntityName>TEST</LegalEntityName> AFTER (/OrganisationMetaData/ExamPrepAssessModeFromAddress)[1]') where ID = 1
Remember XML is always case-sensitive, eg:
use tempdb
go
drop table if exists organisations
go
create table organisations(id int, configuration xml)
insert into organisations(id, configuration) values (1,'<OrganisationMetaData><ExamPrepAssessModeFromAddress></ExamPrepAssessModeFromAddress></OrganisationMetaData>')
update organisations Set configuration.modify('insert <LegalEntityName>TEST</LegalEntityName> after (/OrganisationMetaData/ExamPrepAssessModeFromAddress)[1]') where ID = 1

Copy one table data into another db by using INTO Select

I am getting this error :
Msg 156, Level 15, State 1, Line 39
Incorrect syntax near the keyword 'in'
My code:
SELECT *
INTO EmployeesBackup IN 'DB2.mbd'
FROM Employee
Your syntax is off, and perhaps you intended to do an INSERT INTO ... SELECT:
INSERT INTO DB2.EmployeesBackup
SELECT *
FROM DB1.Employee;
This would work assuming that both databases are on the same server, and that your backup table EmployeesBackup has the same definition as the Employee table.
Try this:
INSERT INTO [DBName].dbo.EmployeesBackup
SELECT * FROM [DBName].dbo.Employee
Please note that, Either both database should be available on the same SQL Server OR if one of them is available on the different server then it should be Linked

Copy table from one server to another using select statement

I need to copy a table from one server to another.for that I have did the below code,
select * into tbls from SNRJDI-32962\xxxmanagement.master.dbo.tbl
When I execute I got error Like,
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '-'.
but this is the actual server name(SNRJDI-32962\xxxmanagement)..Please do needful..
Thank you
You first have to add a linked server from the target server to the source server.
Then you can use a four-part name, separated by dots:
select * into [newtable] from [linked_server].[databasename].dbo.[tablename]
I would add to Andomar's answer that to have special characters in an object name, you need to surround the name in [square brackets] otherwise sql will interpret your "-" as a minus sign

MS SQL Server 2005 Copy data from one table to another

Hey all, I am trying to find out how to copy data from one table to another database table. I have two connections to two different databases. Ones called comp-DEV1 and the other SQLTEST. I am currently unable to copy data from my sorce table (SQLTEST) to my destination table (comp-DEV1).
This is the error:
Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '-'.
Query:
INSERT INTO comp-DEV1.EMSSQL.dbo.tblCL
SELECT *
FROM SQLTEST.EMSSQL.dbo.tblCL
WHERE NOT EXISTS(SELECT *
FROM comp-DEV1.EMSSQL.dbo.tblCL
WHERE (SQLTEST.EMSSQL.dbo.tblCL.CID = comp-DEV1.EMSSQL.dbo.tblCL.CID)
)
Any help would be great :o)
David
Try wrapping your database names in brackets, such as:
INSERT INTO [comp-DEV1].EMSSQL.dbo.tblCL
SELECT *
FROM SQLTEST.EMSSQL.dbo.tblCL
WHERE NOT EXISTS(SELECT *
FROM [comp-DEV1].EMSSQL.dbo.tblCL
WHERE (SQLTEST.EMSSQL.dbo.tblCL.CID =
[comp-DEV1].EMSSQL.dbo.tblCL.CID)
)
Run the following statement first to check that you can read the source from the destination server:
SELECT *
FROM [comp-DEV1].EMSSQL.dbo.tblCL
Get that working first then you should be on your way...

Resources