Change SSRS parameter value into column name in query - sql

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.

Related

SQL Query invalid object name issues when using variable

Msg 208, Level 16, State 1, Line 1
Invalid object name 'events'.
select count(distinct(games)) from events
The error message is about that the events table cannot be found. So, please ensure that:
this is the table name
you have access to this table
It will be better to add the schema. For example, sales.events. It is possible that the table is not in your user default schema.

Create table from a table in SQL Server

Below is the query I tried to execute in SQL Server:
CREATE TABLE List
AS
(SELECT * FROM counts)
I get this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('
I did some research on google to understand and isolate the error, but, failed to do so. Could anyone please help me understand the error. Thank you
SQL-Server's syntax to create a table from a query is by adding an into clause:
SELECT *
INTO list
FROM counts

Getting Invalid Column Name in Azure SQL Data Warehouse

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

How to drop the column name which contain dot

Using SQL server 2008
column name is : file.retry
I want to drop the column file.retry. while running the below query getting the exception
ALTER TABLE FILEQ DROP COLUMN file.retry
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '.'.
How to solve this issue. Any one can help please
Surround file.retry in square brackets like so [file.retry] to avoid the interpreter trying to parse it as a table.column identifier.
ALTER TABLE FILEQ DROP COLUMN [file.retry]
Use square brackets around the column name
ALTER TABLE FILEQ DROP COLUMN [file.retry]

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.