Getting Invalid Column Name in Azure SQL Data Warehouse - sql

I created an alias and checked whether alias column is null or not null but alias is not working in sql data warehouse.
select (emp_id) a
from dbo.test b
where a is not null
Msg 207, Level 16, State 1, Line 1
Invalid column name 'a'.

You can't Use alias as simple as in where clause. But there are other ways which you can implement it
Using CTE
CROSS APPLY/OUTER APPLY
There are good examples at here for you to look-out

Related

Change SSRS parameter value into column name in query

I am trying to, without stored procedures, create a SQL query in an SSRS report and take a parameter value and use it as a column name. My question is how to I change the parameter value into the actual column name? I have tried the following query with the table identifier (i.e. t.#FilterColumn1) and without the table identifier (i.e. #FilterColumn1).
DECLARE #FilterColumn1 AS nvarchar(250)
SET #FilterColumn1 = 'Column1'
SELECT DISTINCT
t.#FilterColumn1
FROM [JetNavDwh2017].[dbo].[TableName] t WITH(NOLOCK)
When I do not use the table identifier the result is the the column name I defined. When I use the table identifier I receive the error message below, but I have not found a conclusive way to resolve the issue based on the error. Thank you in advance for your help!
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '#FilterColumn1'.
Msg 319, Level 15, State 1, Line 5
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.

Lookup in other table & get the data

Need your help in getting the below solved.
I have data in tables
1. Extract entire dump
2. Product table
i need a view where in All the dump to display & another column looking up with the Product SKU in product table with and display as eligible. if not not eligible.
Select CompanyLocationId, ProductSku
From ETL_Extract,
Product_Eligibility_List
where ETL_Extract.ProductSku = Product_Eligibility_List.ProductSku
Msg 209, Level 16, State 1, Line 1 Ambiguous column name 'ProductSku'.
Alias your objects, qualify your columns and stop using syntax that has been outdated for 27 years (Bad habits to kick : using old-style JOINs). The error, however, is telling you the problem; ProductSku is ambigious as it is in both the tables ETL_Extract and Product_Eligibility_List. As a result SQL Server doesn't know which you are trying to reference in your SELECT (even if they do have the same value due to the ON clause).
Once you do all the things I initially listed, you get a query that looks like this that should provide you with the dataset you're after:
SELECT E.CompanyLocationId, --Guessed Alias prefix
PEL.ProductSku
FROM ETL_Extract E
INNER JOIN Product_Eligibility_List PEL ON E.ProductSku = PEL.ProductSku;

Select row in SQL using GUID

When creating the table I used
[Key] UNIQUEIDENTIFIER not null DEFAULT NEWSEQUENTIALID(),
I'm trying to use the following to select, I gave up on doing it in C# for now and am entering the query in MyLittleAdmin (the SQL database manager my hosting provider is using)
SELECT *
FROM GameList
WHERE Key = '9abc2cdc-1919-e611-80d3-008cfa5ae917'
I get the following error:
Msg 156, Level 15, State 1, Line number 1
Incorrect syntax near the keyword 'Key'.
I know I have the right GUID, using SELECT * FROM GameList returns the data and I have copied and pasted from it.
Key is a keyword and must be surroundted by string identifiers if you want to use it as a column name (like it is in the column definition):
SELECT *
FROM GameList
WHERE [Key] = '9abc2cdc-1919-e611-80d3-008cfa5ae917'

Left join ON not null columns can't select not null column

Each table has a column RECNUM. They are (decimal(28,0), not null). That is where I am doing my join. I want to select the column DESC in CAUNIT. It is (varchar(28,0), not null). When I run my query I get:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'DESC'. Below is my query:
SELECT CDCLSVC.UNIT_ID,CAUNIT.DESC
FROM CDCLSVC
LEFT JOIN CAUNIT
ON CDCLSVC.RECNUM = CAUNIT.RECNUM
The problem is with DESC column. In SQL Server it is a reserved keyword:
Microsoft SQL Server uses reserved keywords for defining,
manipulating, and accessing databases. Reserved keywords are part of
the grammar of the Transact-SQL language that is used by SQL Server to
parse and understand Transact-SQL statements and batches. Although it
is syntactically possible to use SQL Server reserved keywords as
identifiers and object names in Transact-SQL scripts, you can do this
only by using delimited identifiers.
Possible solution:
Rename column e.g. description
Quote it with []
You could also use aliases to avoid typing full table names:
SELECT cd.UNIT_ID,ca.[DESC]
FROM CDCLSVC cd
LEFT JOIN CAUNIT ca
ON cd.RECNUM = ca.RECNUM

Select syntax as a subquery to insert into

Here's what I'm trying to do in SQL Query Analyzer.
Insert into db_name1.dbo.tb_name1 (select * from db_name2.dbo.tb_name2)
but I keep getting this error message
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ')'.
I wonder what went wrong. Does subquery isn't work in SQL server 2000?
What is the right syntax for this one? I would like to transfer all the values from a tables column of a database to another table of another database without creating any duplicate of its element but merely overwrites the current values.
This is your query as you think it should be interpeted:
Insert into db_name1.dbo.tb_name1
(select * from db_name2.dbo.tb_name2)
This is the query as the compiler sees it:
Insert into db_name1.dbo.tb_name1(select * from db_name2.dbo.tb_name2)
That is, the open parenthese after the table name means "the list of columns is starting here". But you don't have a list of columns. And select is not a valid column name. Hence the error.
This is easily fixed by removing the parentheses:
Insert into db_name1.dbo.tb_name1
select * from db_name2.dbo.tb_name2
However, it is more correct to include the column names being inserted:
Insert into db_name1.dbo.tb_name1(col1 . . .)
select * from db_name2.dbo.tb_name2;
It is a good idea to always include column names in insert statements. If you had done this, you wouldn't have had to ask this question.