Select Statements in SQL Server - sql-server

I am trying to pull up one specific line from a database that I just typed information into. This is the SELECT Statement that I typed in:
SELECT John
FROM FoodLog
It comes up with the following error statement:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'John'.
However if I type the following SELECT Statement, it pulls up all of the information that I typed into the database:
SELECT Person
FROM FoodLog
I can't figure out how to pull up just one particular line of information from the database.

SELECT *
FROM FoodLog
WHERE Person = 'John'

Related

Convert Geometry to Geography

I want to convert geometry to geography in SQL Server; I followed this article:
https://blogs.msdn.microsoft.com/edkatibah/2008/08/19/working-with-invalid-data-and-the-sql-server-2008-geography-data-type-part-1b/
Here is my query:
INSERT INTO gCOMMUNE
SELECT
[dbo].[commune].[ogr_fid],
GEOGRAPHY::STGeomFromWKB(commune.ogr_geometry.STAsBinary(),4326)
FROM [IMMATS].[dbo].[commune]
but when I ran the command to convert I got this error:
Msg 213, Level 16, State 1, Line 26 The name or column number of the values provided does not match the definition of the table.
It looks like gCOMMUNE table has different number of columns than the query for selecting the data to be inserted in it. You must specify the column names in the INSERT INTO statement. Assuming there are columns named id and geom (from your clarification it turns out they are [ogr_fid] and [ogr_geog]), here is how your statement could look like:
INSERT INTO gCOMMUNE([ogr_fid], [ogr_geog])
SELECT
[dbo].[commune].[ogr_fid],
GEOGRAPHY::STGeomFromWKB(commune.ogr_geometry.STAsBinary(),4326)
FROM [IMMATS].[dbo].[commune]

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

Invalid column name when running a query in SQL Server 2008

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.

MS SQL server 2005 - Error while querying with a column name as key

I have a table with a column name as "key". I am unable to filter based on that column
select * from myTable where key='someVal'
I get the following error
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'key'.
I cannot change the column name. How I can circumvent this issue?
It's because key is a keyword. If you have keywords as object names, you need to put them in brackets:
select * from myTable where [key]='someVal'

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