November 2, 2012 at 9:51 am
Hi ,
I want to give variable as column name.
Example:
DECLARE
@COPYDATEDATETIME
,@COPYDATE_END_OF_CURR_MONTHDATETIME
,@COPYDATE_END_OF_1_MONTH_AGODATETIME
,@COPYDATE_END_OF_2_MONTH_AGODATETIME
,@COPYDATE_END_OF_3_MONTH_AGODATETIME
,@TODAYDATETIME
SET @COPYDATE= '10/31/2012'
SET @COPYDATE_END_OF_CURR_MONTH= DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE)+1,0))
SET @COPYDATE_END_OF_1_MONTH_AGO= DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE),0))
SET @COPYDATE_END_OF_2_MONTH_AGO= DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE)-1,0))
SET @COPYDATE_END_OF_3_MONTH_AGO= DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE)-2,0))
SET @TODAY= CONVERT(VARCHAR, GETDATE(), 101)
SELECT
SUM(CASE WHEN MONTH_1 = DATEADD(MM,DATEDIFF(MM,0,@COPYDATE_END_OF_3_MONTH_AGO),0) THEN 1 ELSE 0 END) AS (SELECT @COPYDATE_END_OF_3_MONTH_AGO )
,SUM(CASE WHEN MONTH_1 = DATEADD(MM,DATEDIFF(MM,0,@COPYDATE_END_OF_2_MONTH_AGO),0) THEN 1 ELSE 0 END) AS (SELECT @COPYDATE_END_OF_2_MONTH_AG0 )
,SUM(CASE WHEN MONTH_1 = DATEADD(MM,DATEDIFF(MM,0,@COPYDATE_END_OF_1_MONTH_AGO),0) THEN 1 ELSE 0 END) AS (SELECT @COPYDATE_END_OF_1_MONTH_AGO )
FROM #MAIN_TABLE
GROUP BY MONTH_1
Is it possible ...if not how to get it??
Thanks,
Komal
November 2, 2012 at 10:04 am
That seems like a horrible way to name your columns but in order to do that you will have to use dynamic sql. Make you sure you wrap your column names with [] or it won't work.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 2, 2012 at 10:05 am
It's possible with using Dynamic Sql. Here just a small sample, but you should be able to get your one from here:
-- that could be your proc input parameter:
DECLARE @ColumnName NVARCHAR(100)
SET @ColumnName = 'Name'
------------------------
DECLARE @SQL NVARCHAR(4000)
-- let's call it template:
SET @SQL = N'SELECT ~ FROM sys.tables'
-- note use of QUOTENAME to prevent sql injection, also it will good for your columns name as it wrap them into []
SET @SQL = REPLACE(@SQL,'~',QUOTENAME(@ColumnName))
EXEC sp_executesql @SQL
November 2, 2012 at 10:12 am
Sean Lange (11/2/2012)
That seems like a horrible way to name your columns but in order to do that you will have to use dynamic sql. Make you sure you wrap your column names with [] or it won't work.
As horrible as it seems, I know it can happen.
Once, a developer asked me to use the column names exactly as the user should view them to avoid additional processing and coding at the front end.
This works for reports, not for creating permanent tables.
November 2, 2012 at 10:27 am
Luis Cazares (11/2/2012)
Sean Lange (11/2/2012)
That seems like a horrible way to name your columns but in order to do that you will have to use dynamic sql. Make you sure you wrap your column names with [] or it won't work.As horrible as it seems, I know it can happen.
Once, a developer asked me to use the column names exactly as the user should view them to avoid additional processing and coding at the front end.
This works for reports, not for creating permanent tables.
Yeah I too have that type of requirement and I shudder. Seems like many times they come back and agree with me that the column names are horrible. Of course you still have to know how to tweak that dynamic sql to get those names the first time. 😉
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 2, 2012 at 12:33 pm
I did something like this below:
DECLARE @COPYDATE datetime
,@COPYDATE_END_OF_CURR_MONTH varchar(50)
,@COPYDATE_END_OF_1_MONTH_AGO varchar(50)
,@COPYDATE_END_OF_2_MONTH_AGO varchar(50)
,@COPYDATE_END_OF_3_MONTH_AGO varchar(50)
,@TODAY DATETIME
SET @COPYDATE = '10/31/2012'
SET @COPYDATE_END_OF_CURR_MONTH =convert(varchar(50),DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE)+1,0)),110)
SET @COPYDATE_END_OF_1_MONTH_AGO = convert(varchar(50),DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE),0)),110)
SET @COPYDATE_END_OF_2_MONTH_AGO = convert(varchar(50),DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE)-1,0)),110)
SET @COPYDATE_END_OF_3_MONTH_AGO = convert(varchar(50),DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE)-2,0)),110)
SET @TODAY = CONVERT(VARCHAR, GETDATE(), 101)
declare @sql varchar(2000)
set @sql = N'SELECT
SUM(CASE WHEN MONTH_1 = DATEADD(MM,DATEDIFF(MM,0,@COPYDATE_END_OF_3_MONTH_AGO),0) THEN 1 ELSE 0 END) AS ['+ @COPYDATE_END_OF_3_MONTH_AGO +']
,SUM(CASE WHEN MONTH_1 = DATEADD(MM,DATEDIFF(MM,0,@COPYDATE_END_OF_2_MONTH_AGO),0) THEN 1 ELSE 0 END) AS ['+@COPYDATE_END_OF_2_MONTH_AGO+' ]
,SUM(CASE WHEN MONTH_1 = DATEADD(MM,DATEDIFF(MM,0,@COPYDATE_END_OF_1_MONTH_AGO),0) THEN 1 ELSE 0 END) AS ['+@COPYDATE_END_OF_1_MONTH_AGO+' ]
FROM #MAIN_TABLE
GROUP BY MONTH_1'
EXEC sp_executesql @sql
Getting ERROr :
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.
November 3, 2012 at 11:04 am
CELKO (11/2/2012)
I want to give variable as column name.
NO! Please read just one book on RDBMS before you try writing SQL. A column is an attribute of an entity, shown with a scalar value drawn from a domain that is modeled with a single data type.
Renaming columns is magical thinking. It would be like changing lead into gold. Creating new attributes on the fly is also magical thinking. You wave your wand and the elephant grows wings!
Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML. The report period table gives a name to a range of dates that is common to the entire enterprise.
CREATE TABLE Report_Periods
(report_name VARCHAR(30) NOT NULL PRIMARY KEY,
report_start_date DATE NOT NULL,
report_end_date DATE NOT NULL,
CONSTRAINT date_ordering
CHECK (report_start_date <= report_end_date),
etc);
These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantage is that it will sort with the ISO-8601 data format required by Standard SQL.
He's not building a table, Joe. He's trying to build a dynamic cross-tab report.
Based on teaching SQL for a few decades is that you will need 2-3 years of hard work before you are able to write usable SQL code.
Not if you have a good teacher.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 3, 2012 at 11:06 am
komal145 (11/2/2012)
EXEC sp_executesql @sqlGetting ERROr :
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.
The error says exactly what the problem is here. Change @SQL to be an NVARCHAR variable instead of VARCHAR.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 3, 2012 at 11:19 am
komal145 (11/2/2012)
I did something like this below:DECLARE @COPYDATE datetime
,@COPYDATE_END_OF_CURR_MONTH varchar(50)
,@COPYDATE_END_OF_1_MONTH_AGO varchar(50)
,@COPYDATE_END_OF_2_MONTH_AGO varchar(50)
,@COPYDATE_END_OF_3_MONTH_AGO varchar(50)
,@TODAY DATETIME
SET @COPYDATE = '10/31/2012'
SET @COPYDATE_END_OF_CURR_MONTH =convert(varchar(50),DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE)+1,0)),110)
SET @COPYDATE_END_OF_1_MONTH_AGO = convert(varchar(50),DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE),0)),110)
SET @COPYDATE_END_OF_2_MONTH_AGO = convert(varchar(50),DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE)-1,0)),110)
SET @COPYDATE_END_OF_3_MONTH_AGO = convert(varchar(50),DATEADD(DAY,-1,DATEADD(MM, DATEDIFF(M,0,@COPYDATE)-2,0)),110)
SET @TODAY = CONVERT(VARCHAR, GETDATE(), 101)
declare @sql varchar(2000)
set @sql = N'SELECT
SUM(CASE WHEN MONTH_1 = DATEADD(MM,DATEDIFF(MM,0,@COPYDATE_END_OF_3_MONTH_AGO),0) THEN 1 ELSE 0 END) AS ['+ @COPYDATE_END_OF_3_MONTH_AGO +']
,SUM(CASE WHEN MONTH_1 = DATEADD(MM,DATEDIFF(MM,0,@COPYDATE_END_OF_2_MONTH_AGO),0) THEN 1 ELSE 0 END) AS ['+@COPYDATE_END_OF_2_MONTH_AGO+' ]
,SUM(CASE WHEN MONTH_1 = DATEADD(MM,DATEDIFF(MM,0,@COPYDATE_END_OF_1_MONTH_AGO),0) THEN 1 ELSE 0 END) AS ['+@COPYDATE_END_OF_1_MONTH_AGO+' ]
FROM #MAIN_TABLE
GROUP BY MONTH_1'
EXEC sp_executesql @sql
Getting ERROr :
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.
I noticed that all of your date variables are VARCHAR which could cause some computational errors and some "implicit conversion" performance problems. I'd like to recommend that you post the CREATE TABLE statement for your #MAIN_TABLE and post some readily consumable data with it so we can help a little better. [font="Arial Black"]Please see the article at the first link in my signature line below for how to do that correctly. [/font]
--Jeff Moden
Change is inevitable... Change for the better is not.
November 3, 2012 at 12:52 pm
There's not always a report writer to be had or allowed to be had, Joe. Learning how to do a cross tab on all sorts of data is an essential skill.
While I agree with everything you said in your latest post above, there was no need for you to bust the OPs chops in the post previous to that nor do I agree that it takes 2 to 3 years to teach someone to be really "useful" at T-SQL. Your comments were inappropriately negative. If you spent less time being negative with the student, there'd be more time to teach and exercise the student. Take the negativity out and you could probably get it down to 6 months or so.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 3, 2012 at 2:03 pm
Jeff Moden (11/3/2012)
... Take the negativity out and you could probably get it down to 6 months or so.
+100
November 4, 2012 at 11:56 am
CELKO (11/3/2012)
He's not building a table, Joe. He's trying to build a dynamic cross-tab report.
That is even worse; he does not know how to use a report writer
What really amazes me about this comment is that your book (SQL for Smarties - Advanced SQL Programming - Third Edition) speaks specificially of why you might want to do a crosstab. I quote from Section 23.7 with the title of "Cross Tablulations" on page 538...
However, if you have to use the reporting package on the client side,
the extra time required to transfer data will make these methods on the
server side much faster.
So stop badmouthing the OP on what you, yourself, have justified in writing. At least read your own book before going negative on someone! 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
November 5, 2012 at 6:02 am
CELKO (11/4/2012)
2) When the SQL is up to the task and efficient, do it in the database. In recent years that has meant more basic aggregation has been put into queries (rollup, cube, et al).
That sometimes requires the naming of columns, Joe. There's no harm in doing so, either. The columns have to be named one way or the other.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply