Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Issue with View in SQL2012

Author  Topic 

ryankeast
Starting Member

9 Posts

Posted - 2013-11-08 : 03:31:37
Hi there,

I am having an issue with a script and I have got to the bottom of the troublesome part, but am unsure how to fix it.

When I run the below code in New Query window in Management Studio it works as I expect. When I execute it in a view in order to save it to attach it to a refreshable spreadsheet it fails.

The code is -

SELECT     MAX(WK.[TRASWEEK]) AS TRASWEEK
FROM (SELECT DISTINCT [TRANS-WEEK] AS 'TRASWEEK'
FROM dbo.[IH_RE-TNCY-TRANS] AS TRA6
WHERE ([TRANS-DATE] > GETDATE() - 42) AND ([ACCOUNT-TYPE] = 'IN')
GROUP BY [TRANS-WEEK]) AS WK


When I run this in New Query. I get one result (correctly) of 27. 27 being the maximum week.

When I paste this in the view and execute it it changes the code to -

SELECT     MAX('TRASWEEK') AS Expr1
FROM (SELECT DISTINCT [TRANS-WEEK] AS 'TRASWEEK'
FROM dbo.[IH_RE-TNCY-TRANS] AS TRA6
WHERE ([TRANS-DATE] > GETDATE() - 42) AND ([ACCOUNT-TYPE] = 'IN')
GROUP BY [TRANS-WEEK]) AS WK


This pulls me back just a result of "Trasweek" and not the 27 I would have hoped for. Why is 2012 doing this. Why is it changing my code?

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-08 : 04:32:07
DOnt use view editor. Create the code using CREATE VIEW syntax in SSMS editor itself.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -