﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Give Varibale as column name / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 07:11:34 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote][b]CELKO (11/4/2012)[/b][hr]2) When the SQL is up to the task [i]and efficient,[/i] do it in the database. In recent years that has meant more basic aggregation has been put into queries (rollup, cube, et al).[/quote]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.</description><pubDate>Mon, 05 Nov 2012 06:02:56 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote] I quote from Section 23.7 with the title of "Cross Tabulations" 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 theserver side much faster.[/quote]So stop badmouthing the OP on what you, yourself, have justified in writing.  At least read your own book before going negative on someone! ;-)[/quote]1) I never advocate formatting the presentation in the DB layer of a tiered architecture. This guy wants to name columns! It is fine to assemble the data, but not to format it. If I did, I need to correct my next edition. Return a table, but not a display. These days, I find that I do not want even to have an ORDER BY to convert the result table into a sequential file structure for the front end. I am talking to a report server that will sort, compute, aggregate, colorize, etc a dozen different ways for a zillion reports. They eat and digest raw data better  than than I can in SQL. 2) When the SQL is up to the task [i]and efficient,[/i] do it in the database. In recent years that has meant more basic aggregation has been put into queries (rollup, cube, et al).3) SQL FOR SMARTIES is in the 4-th edition now. The typesetting stinks. It was outsourced to India, crammed into one template for all MKP books and I have offered to pay for re-setting it.  </description><pubDate>Sun, 04 Nov 2012 22:08:00 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote][b]CELKO (11/3/2012)[/b][hr][quote] He's not building a table, Joe.  He's trying to build a dynamic cross-tab report.  [/quote]That is even worse; he does not know how to use a report writer[/quote]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... [quote]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 theserver side much faster.[/quote]So stop badmouthing the OP on what you, yourself, have justified in writing.  At least read your own book before going negative on someone! ;-)</description><pubDate>Sun, 04 Nov 2012 11:56:49 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote][b]Jeff Moden (11/3/2012)[/b][hr]... Take the negativity out and you could probably get it down to 6 months or so.[/quote]+100</description><pubDate>Sat, 03 Nov 2012 14:03:31 GMT</pubDate><dc:creator>Eugene Elutin</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>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.</description><pubDate>Sat, 03 Nov 2012 12:52:11 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote] He's not building a table, Joe.  He's trying to build a dynamic cross-tab report.  [/quote]That is even worse; he does not know how to use a report writer[quote][quote]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.[/quote] Not if you have a good teacher. [/quote]The student has to bring something. Today, there are a lot of kids who expect you "learn me some SQL" rather than the truth that "I teach, you learn".What I found is that if I know their native programming language (or at least the language family), I know why they are doing what they do. People seldom make [i]random[/i] errors. For example nouns in Japanese do not change form and the verbs and quasi-verbs (wa, ga, ka, etc) go at the end of the sentence. Compare that to English or (worse) strongly inflectional languages like Latin.  So the Japanese speaker will forget plurals and cases, then put his verb in the wrong place. Oh, the Japanese temporal model is different for verb tenses. A German speaker has a much easier time with English. Some of the words are common or close enough. Sentences have a subject, the verb tenses are pretty much the same, etc. You simply expect the Japanese student to have a harder time than the German. About 3-5 years difference in learning time, if I remember teh stats. Same principle for RDBMS and SQL.The only other declarative language most people know is Spreadsheet and that is not much help with SQL. The guys that really "get it" from the start are LISP and APL programmers!  </description><pubDate>Sat, 03 Nov 2012 12:02:03 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote][b]komal145 (11/2/2012)[/b][hr]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 @sqlGetting ERROr : Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.[/quote]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]</description><pubDate>Sat, 03 Nov 2012 11:19:31 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote][b]komal145 (11/2/2012)[/b][hr]EXEC sp_executesql @sqlGetting ERROr : Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.[/quote]The error says exactly what the problem is here.  Change @SQL to be an NVARCHAR variable instead of VARCHAR.</description><pubDate>Sat, 03 Nov 2012 11:06:01 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote][b]CELKO (11/2/2012)[/b][hr][quote]I want to give variable as column name.[/quote]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 &amp;lt;= 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. [/quote]He's not building a table, Joe.  He's trying to build a dynamic cross-tab report.  [quote]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.[/quote]Not if you have a good teacher. </description><pubDate>Sat, 03 Nov 2012 11:04:04 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote]I want to give variable as column name.[/quote]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 &amp;lt;= 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. You still think in monolithic procedural code where the data and process are welded into a single module. 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. It is like learning a foreign language in a different language family from your native one. </description><pubDate>Fri, 02 Nov 2012 12:33:22 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>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 @sqlGetting ERROr : Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.</description><pubDate>Fri, 02 Nov 2012 12:33:04 GMT</pubDate><dc:creator>komal145</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote][b]Luis Cazares (11/2/2012)[/b][hr][quote][b]Sean Lange (11/2/2012)[/b][hr]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.[/quote]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.[/quote]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. ;-)</description><pubDate>Fri, 02 Nov 2012 10:27:00 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>[quote][b]Sean Lange (11/2/2012)[/b][hr]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.[/quote]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.</description><pubDate>Fri, 02 Nov 2012 10:12:39 GMT</pubDate><dc:creator>Luis Cazares</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>It's possible with using Dynamic Sql. Here just a small sample, but you should be able to get your one from here:[code="sql"]-- 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[/code]</description><pubDate>Fri, 02 Nov 2012 10:05:24 GMT</pubDate><dc:creator>Eugene Elutin</dc:creator></item><item><title>RE: Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>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.</description><pubDate>Fri, 02 Nov 2012 10:04:52 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>Give Varibale as column name</title><link>http://www.sqlservercentral.com/Forums/Topic1380472-392-1.aspx</link><description>Hi ,     I want to give  variable  as column name.Example:DECLARE @COPYDATE					DATETIME ,@COPYDATE_END_OF_CURR_MONTH	DATETIME,@COPYDATE_END_OF_1_MONTH_AGO	DATETIME,@COPYDATE_END_OF_2_MONTH_AGO	DATETIME,@COPYDATE_END_OF_3_MONTH_AGO	DATETIME,@TODAY							DATETIMESET @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)SELECTSUM(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_TABLEGROUP BY MONTH_1Is it possible ...if not how to get it??Thanks,Komal</description><pubDate>Fri, 02 Nov 2012 09:51:55 GMT</pubDate><dc:creator>komal145</dc:creator></item></channel></rss>