Alter View not recognizing column - tsql
I have scripts for adding columns to tables which worked.
When I run scripts to alter views with the new column the script fails because the columns are not recognized
Msg 207, Level 16, State 1, Procedure UniqueTempDispositions, Line 76
Invalid column name 'servicerequestid'.
Msg 207, Level 16, State 1, Procedure UniqueTempDispositions, Line 47
Invalid column name 'servicerequestid'.
Msg 207, Level 16, State 1, Procedure MergeDispositions, Line 54
Invalid column name 'ServiceRequestID'.
Msg 207, Level 16, State 1, Procedure NonPIICachedDispositions, Line 18
Invalid column name 'ServiceRequestID'.
Any reason why? Am I missing something?
I've started and stopped the server, I've relogged in to no avail.
ORIGINAL SCRIPT:
alter view
dbo.UniqueTempDispositions
as
SELECT QuotaGroup, Country, ServiceGroup, Language, ContactChannel, TrackingID, CONVERT(DATETIME, CaseClosedDate) AS casecloseddate, MSFTRep, CustEmail,
CustPhone, CustomerName, ProductFamily, ProductSubType, CONVERT(DATETIME, CandidateReceivedDate) AS candidatereceiveddate, CONVERT(INT, SurveyMode)
AS surveymode, CONVERT(DATETIME, SurveyWaveStartDate) AS surveywavestartdate, CONVERT(DATETIME, SurveyInvitationDate) AS surveyinvitationdate,
CONVERT(DATETIME, SurveyReminderDate) AS surveyreminderdate, CONVERT(DATETIME, SurveyCompleteDate) AS surveycompletedate, CONVERT(DATETIME,
OptOutDate) AS optoutdate, CONVERT(DATETIME, SurveyWaveEndDate) AS surveywaveenddate, CONVERT(INT, DispositionCode) AS dispositioncode, SurveyName,
SurveyVendor, COUNT(*) AS countofunique, BusinessUnitName, UploadId, MIN(LineNumber) AS LineNumber, BusinessUnitSubgroup, ServiceRequestID
FROM DispositionReporting.dbo.tempDispositions AS td
GROUP BY QuotaGroup, Country, ServiceGroup, Language, ContactChannel, TrackingID, CONVERT(DATETIME, CaseClosedDate), MSFTRep, CustEmail, CustPhone,
CustomerName, ProductFamily, ProductSubType, CONVERT(DATETIME, CandidateReceivedDate), CONVERT(INT, SurveyMode), CONVERT(DATETIME,
SurveyWaveStartDate), CONVERT(DATETIME, SurveyInvitationDate), CONVERT(DATETIME, SurveyReminderDate), CONVERT(DATETIME, SurveyCompleteDate),
CONVERT(DATETIME, OptOutDate), CONVERT(DATETIME, SurveyWaveEndDate), CONVERT(INT, DispositionCode), SurveyName, SurveyVendor, BusinessUnitName,
UploadId, BusinessUnitSubgroup, ServiceRequestID
go
I ended up dropping and re-adding the view which worked, just don't understand the other way around:
use DispositionReporting
go
drop view dbo.UniqueTempDispositions
go
create view
dbo.UniqueTempDispositions
as
SELECT QuotaGroup, Country, ServiceGroup, Language, ContactChannel, TrackingID, CONVERT(DATETIME, CaseClosedDate) AS casecloseddate, MSFTRep, CustEmail,
CustPhone, CustomerName, ProductFamily, ProductSubType, CONVERT(DATETIME, CandidateReceivedDate) AS candidatereceiveddate, CONVERT(INT, SurveyMode)
AS surveymode, CONVERT(DATETIME, SurveyWaveStartDate) AS surveywavestartdate, CONVERT(DATETIME, SurveyInvitationDate) AS surveyinvitationdate,
CONVERT(DATETIME, SurveyReminderDate) AS surveyreminderdate, CONVERT(DATETIME, SurveyCompleteDate) AS surveycompletedate, CONVERT(DATETIME,
OptOutDate) AS optoutdate, CONVERT(DATETIME, SurveyWaveEndDate) AS surveywaveenddate, CONVERT(INT, DispositionCode) AS dispositioncode, SurveyName,
SurveyVendor, COUNT(*) AS countofunique, BusinessUnitName, UploadId, MIN(LineNumber) AS LineNumber, BusinessUnitSubgroup, ServiceRequestID
FROM DispositionReporting.dbo.tempDispositions AS td
GROUP BY QuotaGroup, Country, ServiceGroup, Language, ContactChannel, TrackingID, CONVERT(DATETIME, CaseClosedDate), MSFTRep, CustEmail, CustPhone,
CustomerName, ProductFamily, ProductSubType, CONVERT(DATETIME, CandidateReceivedDate), CONVERT(INT, SurveyMode), CONVERT(DATETIME,
SurveyWaveStartDate), CONVERT(DATETIME, SurveyInvitationDate), CONVERT(DATETIME, SurveyReminderDate), CONVERT(DATETIME, SurveyCompleteDate),
CONVERT(DATETIME, OptOutDate), CONVERT(DATETIME, SurveyWaveEndDate), CONVERT(INT, DispositionCode), SurveyName, SurveyVendor, BusinessUnitName,
UploadId, BusinessUnitSubgroup, ServiceRequestID
go
exec sp_refreshview 'dbo.UniqueTempDispositions'
go
Take a look at how to make sure that the view will have the underlying table changes by using sp_refreshview
Did you run sp_refreshview against the views after you made changes to the tables?
Related
PostgreSQL Time Dimension (By Hours and Days) Error
I am am building a Time Dimension table in PostgreSQL with DATE_ID and DATE_DESC. My T-SQL (works perfectly) script is: set DATEFIRST 1 ;WITH DATES AS ( SELECT CAST('2019-01-01 00:00:00.000' AS datetime) AS [DATE] UNION ALL SELECT DATEADD(HH,1,[DATE]) FROM DATES WHERE DATEADD(HH,1,[DATE]) <= CAST('2019-12-31' AS datetime) ) SELECT DATE_ID, DATE_DESC from ( SELECT CONVERT(int, CONVERT(char(8), DATE, 112)) AS DATE_ID, DATE AS DATE_DESC FROM DATES)a order by 1 OPTION (MAXRECURSION 0) At the moment Im trying to convert this code to PostgreSQL readable one and it does not work.. Here is mine at the moment: set EXTRACT(DOW FROM TIMESTAMP '2019-01-01 00:00:00.000')+1 ;WITH DATES AS ( SELECT CAST('2019-01-01 00:00:00.000' AS timestamp) AS DATE UNION ALL SELECT CURRENT_DATE + INTERVAL '1 hour' FROM DATES WHERE CURRENT_DATE + INTERVAL '1 hour' <= CAST('2019-12-31' AS timestamp) ) SELECT DATE_ID, DATE_DESC from (SELECT cast(to_char((DATE)::TIMESTAMP,'yyyymmddhhmiss') as BIGInt) AS DATE_ID, DATE AS DATE_DESC FROM DATES)a order by 1 OPTION (MAXRECURSION 0) I need all the hours (24h) between 2019-01-01 and 2019-12-31 . At the moment I think OPTION (MAXRECURSION 0) and set EXTRACT(DOW FROM TIMESTAMP '2019-01-01 00:00:00.000')+1 is not working properly.
Its a problem of Recursive CTE, In Postgresql, your desired query will be like below WITH recursive DATES AS ( SELECT CAST('2019-01-01 00:00:00.000' AS timestamp) AS date_ UNION ALL SELECT date_ + INTERVAL '1 hour' FROM DATES WHERE date_ + INTERVAL '1 hour' <= CAST('2019-12-31' AS timestamp) ) SELECT DATE_ID, DATE_DESC from (SELECT cast(to_char((date_)::TIMESTAMP,'yyyymmddhhmiss') as BIGInt) AS DATE_ID, date_ AS DATE_DESC FROM DATES)a order by 1 DEMO
CONVERT function from SQL Server to REDSHIFT
This is the code from SQL Server; I need to convert it into redshift convert(decimal, LEFT(convert(varchar, dateadd(month, 3, convert(datetime, convert(varchar, Column_name))), 112), 6)) Any help?
You can convert it by replacing the convert(varchar function with TO_CHAR and specifying the output format as'YYYMMDD': -- SQL Server original convert(decimal, LEFT(convert(varchar, dateadd(month, 3, convert(datetime, convert(varchar, Column_name))), 112 ), 6)) -- Redshift version convert(decimal, LEFT( TO_CHAR( dateadd(month, 3, convert(datetime, convert(varchar, '20190102'))), 'YYYMMDD'), 6)) -- Output: 19040 The original uses CONVERT with the SQL Server specific 3rd parameter 112 to convert the date to the format of YYYYMMDD. The Redshift equivalent is TO_CHAR.
INSERT UNION POC results into tel
I would like to ALTER a proc so I can insert results into table. Can you please guide? thank you.. ALTER proc [dbo].[usp_CtotalPPPP] AS BEGIN SELECT CID, Consumer1, Consumer2, datepart(year, getdate()) Year, 'Jan' Month, [Jan_S] Budget, [JanAct] Act, getdate() FROM CProgramDetails UNION SELECT CID, Consumer1, Consumer2, datepart(year, getdate()) Year, 'Feb' Month, [FEBOCV_Spend] Budget, [FEBAct] Act, getdate() FROM CProgramDetails UNION SELECT CID, Consumer1, Consumer2, date part(year, getdate()) Year, 'Mar' Month, [MarOCV_Spend] Budget, [MarAct] Act, getdate() FROM CProgramDetails UNION SELECT CID, Consumer1, Consumer2, datepart(year, getdate()) Year, 'Apr' Month, [AprOCV_Spend] Budget, [AprAct] Act, getdate() FROM CProgramDetails INSERT INTO [dbo].[ABCD123] (---this is failing - I do not know where to add this) End
IF [dbo].[ABCD123] already exists INSERT INTO [dbo].[ABCD123] Select ... Union Select ... IF [dbo].[ABCD123] does NOT exist and you want to create it on-the-fly SELECT CID , Consumer1 , Consumer2 , datepart(year, getdate()) Year , 'Jan' Month , [Jan_S] Budget , [JanAct] Act , getdate() INTO [dbo].[ABCD123] --<< only once in the top query FROM CProgramDetails UNION SELECT ... UNION SELECT
SQL OVER(Partition) issue - selecting a subset
I'm trying to get a specific subset of data using "over(partition)" syntax. I've created sample data to illustrate. Running the following CTE results in the a small example result set. I need to calculate 2 date ranges using the following definitions/pseudocode 1: Days to close = CloseDate where stat = 'Closed' minus opendate partitioned by problem 2: Days to solve = "ClosedDate where stat = 'Solved' minus opendate partitioned by problem. I can get #1 using the over(partition) syntax, but I cannot figure out #2. with cte as ( select 114110712007835 as 'SRNumber', 214110712007835004 as ProblemNumber, 'Open' as 'Stat', 314110712007835004001 as TaskNumber, convert(datetime, '2015-03-02 19:47:43',120) as OpenDate, convert(datetime, '2015-03-03 19:36:37',120) as CloseDate union select 114110712007835 as 'SRNumber', 214110712007835004 as ProblemNumber, 'Investigate' as 'stat', 314110712007835004002 as TaskNumber, convert(datetime, '2015-03-04 00:29:13',120) as OpenDate, convert(datetime, '2015-03-05 19:36:34',120) as CloseDate union select 114110712007835 as 'SRNumber', 214110712007835004 as ProblemNumber, 'Solve' as 'stat', 314110712007835004003 as TaskNumber, convert(datetime, '2015-03-06 18:17:13',120) as OpenDate, convert(datetime, '2015-03-07 13:07:31',120) as CloseDate union select 114110712007835 as 'SRNumber', 214110712007835004 as ProblemNumber, 'Close' as 'stat', 315032012542588001001 as TaskNumber, convert(datetime, '2015-03-08 15:24:34',120) as OpenDate, convert(datetime, '2015-03-09 15:15:42',120) as CloseDate union select 114110712007835 as 'SRNumber', 215032012542588001 as ProblemNumber, 'Open' as 'stat', 315032012542588001002 as TaskNumber, convert(datetime, '2015-04-20 20:05:48',120) as OpenDate, convert(datetime, '2015-04-21 03:24:24',120) as CloseDate union select 114110712007835 as 'SRNumber', 215032012542588001 as ProblemNumber, 'Investigate' as 'stat', 315032012542588001003 as TaskNumber, convert(datetime, '2015-04-22 18:55:03',120) as OpenDate, convert(datetime, '2015-04-23 03:24:28',120) as CloseDate union select 114110712007835 as 'SRNumber', 215032012542588001 as ProblemNumber, 'Solve' as 'stat', 315032012542588001004 as TaskNumber, convert(datetime, '2015-04-24 13:35:24',120) as OpenDate, convert(datetime, '2015-04-27 02:24:31',120) as CloseDate union select 114110712007835 as 'SRNumber', 215032012542588001 as ProblemNumber, 'Close' as 'stat', 315032012542588001004 as TaskNumber, convert(datetime, '2015-04-26 13:35:24',120) as OpenDate, convert(datetime, '2015-04-29 03:24:31',120) as CloseDate ) select srnumber, problemnumber, stat, opendate, closedate, --min(opendate) over(partition by problemnumber) as MinDate, max(closedate) over(partition by problemnumber) as MaxDate case when stat = 'Solve' then datediff(mi,min(opendate) over(partition by problemnumber),max(closedate) over(partition by problemnumber)) else NULL end as Days_to_Solve ,datediff(mi,min(opendate) over(partition by problemnumber),max(closedate) over(partition by problemnumber)) as Days_to_Close from cte order by problemnumber asc, opendate asc go
A subquery could get you the result you need. SELECT srnumber , problemnumber , stat , opendate , closedate , --min(opendate) over(partition by problemnumber) as MinDate, max(closedate) over(partition by problemnumber) as MaxDate ( CASE WHEN stat = 'Solve' THEN ( SELECT TOP 1 DATEDIFF(mi, MIN(opendate) OVER ( ), MAX(closedate) OVER ( )) FROM cte c WHERE c.problemnumber = cte.problemnumber AND c.CloseDate <= cte.CloseDate ) ELSE NULL END ) AS Days_to_Solve , DATEDIFF(mi, MIN(opendate) OVER ( PARTITION BY problemnumber ), MAX(closedate) OVER ( PARTITION BY problemnumber )) AS Days_to_Close FROM cte ORDER BY problemnumber ASC , opendate ASC
My results very closely resemble user1221684's, but I use windows functions instead of a subquery. So my answer should run a little more efficiently and it's simpler. Check it out: SELECT *, DATEDIFF( MI, --this is minutes. For days switch "MI" to "DAY" MIN(CASE WHEN [Stat] = 'Open' THEN OpenDate END) OVER (PARTITION BY problemNumber), MIN(CASE WHEN [Stat] = 'Solve' THEN CloseDate END) OVER (PARTITION BY problemNumber) ) AS Minutes_To_Solve , DATEDIFF( MI, --this is minutes. For days switch "MI" to "DAY" MIN(CASE WHEN [stat] = 'Open' THEN OpenDate END) OVER (PARTITION BY problemNumber), MIN(CASE WHEN [Stat] = 'Close' THEN CloseDate END) OVER (PARTITION BY problemNumber) ) AS Minutes_To_Close FROM CTE ORDER BY OpenDate
use of IsNull with date
I have the following TSQL code: Declare #MyDate datetime Select #MyDate = ISNULL(T.requireddate, Convert(DateTime, '01/01/2013', 101)) from myTable T where T.somekey = somevalue Select #MyDate The output is NULL. Why isn't it 01/01/2013?
Are you sure that select returns any rows? If that select returns no rows then #MyDate would be null Try Select T.requireddate, ISNULL(T.requireddate, Convert(DateTime, '01/01/2013', 101)) from myTable T where T.somekey = somevalue