tsql : how do you query with dashes in column names? - sql-server

I have the query
select * from products p, products_temp t
where p.ManufacturerPartNumber = t.[INV-PRICE-VENDOR-PART]
where the column names have dashes in them which SQL Server 2005 seems to automatically add brackets to. What is the correct way of accessing this in a query? I've tried with brackets and without the brackets and just end up with errors.
the error I get from sql mgmt studio is
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'where'.
thanks in advance

that is because you have repeated WHERE twice in your statement. Got nothing to do with the square brackets which you will need because of the dashes.

use "current" join syntax:
SELECT
*
from products p
INNER JOIN products_temp t ON p.ManufacturerPartNumber = t.[INV-PRICE-VENDOR-PART]

Related

Microsoft SQL Server: Error with Group By

I'm new to Microsoft SQL Server 2014. I run this SQL code:
SELECT TOP(10) 'DBSG' as seek_entity, *
FROM DBSG..PM00200
and get this result:
Next, I want to find out total line items for that entity with code below.
WITH vw_pm00200_all AS
(
SELECT TOP(10)
'DBSG' as seek_entity, *
FROM
DBSG..PM00200
)
SELECT
seek_entity,
COUNT(*) AS total
FROM
vw_pm00200_all
GROUP BY
1
Sadly, I get this error. I have no idea why it failed.
Msg 164, Level 15, State 1, Line 9
Each GROUP BY expression must contain at least one column that is not an outer reference.
Lastly, please advise is Microsoft SQL Server based on Transact-SQL?
It looks like you are running into this problem here: Each GROUP BY expression must contain at least one column that is not an outer reference
As the answer points out, grouping by a constant literal is pointless as it is the same for all results. Count(*) will return the same result as Count(*) with a GROUP BY.
If this is just test code and you plan on using a CASE statement (with different values) in place of the string literal, you may have better luck.
Yes, T-SQL is Microsoft SQL Server's flavor of SQL.

how to use with and inner join in Sql server query?

Why this query must be incorrect ? :
with a as
(
select *
from justification_game_publisher
inner join justifications
on justifications.id = justification_id
where game_id=1594
)
select *
from games_publisher_class
inner join a
on a.game_id = games_publisher_class.game_id
and a.publisher_id = games_publisher_class.publisher_id
sql server says :
Msg 319, Level 15, State 1, Line 2: Incorrect syntax near the keyword
'with'. If this statement is a common table expression, an
xmlnamespaces clause or a change tracking context clause, the previous
statement must be terminated with a semicolon.
I think you have additional sql expression before.
Try
;with a as (....
instead of
with a as (....
I hope this should help :)

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'

SQL error - Incorrect Syntax near Keyword 'IS' - Issue with Database Name in SQL?

I am trying to execute a very simple SQL query using "Microsoft Query". I can connect to the tables, select the columns I need but when I try to execute I get error msg saying "Incorrect Syntax near keyword 'IS'"
I got the SQL statement below through automated query but it just doesn't execute successfully. I think I know what the issue is. It is because my database catalog name is "IS". I tried executing same query on my other databases with different names and it works fine. Since I have access to several databases I need to specifiy which db I am accessing in my script and that's when it causes this issue. Is there a work around in my situation where I can avoid using database name and perhaps declare a variable?
SELECT Table1.id,
Table1.Name,
Table1.Status,
Table1.DateEntered
FROM IS.dbo.Table1 Table1
OR
SELECT * FROM IS.dbo.Table1 Table1 (Same error msg)
IS is a SQL reserved keyword, you have to wrap it with []
SELECT * FROM [IS].dbo.Table1 Table1 (Same error msg)
however, is a good practice - and error avoiding technique - to name tables without using reserved keywords or to always use brackets around tables name
I'd assume IS is a reserved keyword. Try wrapping it around square brackets:
SELECT Table1.id, Table1.Name, Table1.Status, Table1.DateEntered FROM [IS].dbo.Table1 Table1

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

Resources