From jwcolby at gmail.com Thu Oct 8 10:50:51 2015 From: jwcolby at gmail.com (John Colby) Date: Thu, 8 Oct 2015 11:50:51 -0400 Subject: [dba-SQLServer] Stored Procedure help Message-ID: I have a stored procedure where I built up a bunch of common table expressions to do various things related to counting data. At the bottom I have a bunch of Select statements which I would like to select ONE from by passing in a parameter. ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), @ToYrMo as char(6) A bunch of CTEs here --SELECT * FROM cteMoveNewOld --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast --SELECT * from cteMoveLastWithDistance SELECT * FROM cteMoveLastDistance_GrpBySt_DistanceCode --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode --Select * from cteMoveLastDistance_GrpByStNew_StOld My current method is to come in and uncomment one line, run the SP, comment that out, uncomment another, run that. So I want to add a new input variable to pass in a report name and use that to select one of the CTEs. ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), @FromYrMo as char(6), @ToYrMo as char(6) AFAICT the ONLY thing I can do as the last line of the SP is a select, which is why I currently uncomment one of the lines to run it. Is there any way to use an IF as the last line (block) to select one of the selects to return data? -- John W. Colby Colby Consulting From scott.marcus at tsstech.com Thu Oct 8 11:13:58 2015 From: scott.marcus at tsstech.com (Scott Marcus) Date: Thu, 8 Oct 2015 12:13:58 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: John, IF @variable = 'First' BEGIN SELECT * FROM cteMoveNewOld END ELSE IF @variable = 'Second' BEGIN SELECT * FROM cteMoveNewOldWithRowNum END ELSE IF... You get the picture -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of John Colby Sent: Thursday, October 8, 2015 11:51 AM To: Discussion concerning MS SQL Server Subject: [dba-SQLServer] Stored Procedure help I have a stored procedure where I built up a bunch of common table expressions to do various things related to counting data. At the bottom I have a bunch of Select statements which I would like to select ONE from by passing in a parameter. ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), @ToYrMo as char(6) A bunch of CTEs here --SELECT * FROM cteMoveNewOld --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast --SELECT * from cteMoveLastWithDistance SELECT * FROM cteMoveLastDistance_GrpBySt_DistanceCode --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode --Select * from cteMoveLastDistance_GrpByStNew_StOld My current method is to come in and uncomment one line, run the SP, comment that out, uncomment another, run that. So I want to add a new input variable to pass in a report name and use that to select one of the CTEs. ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), @FromYrMo as char(6), @ToYrMo as char(6) AFAICT the ONLY thing I can do as the last line of the SP is a select, which is why I currently uncomment one of the lines to run it. Is there any way to use an IF as the last line (block) to select one of the selects to return data? -- John W. Colby Colby Consulting _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com NOTICE: This electronic mail transmission is for the use of the named individual or entity to which it is directed and may contain information that is privileged or confidential. If you are not the intended recipient, any disclosure, copying, distribution or use of the contents of any information contained herein is prohibited. If you have received this electronic mail transmission in error, delete it from your system without copying or forwarding it, and notify the sender of the error by replying via email or calling TSS Technologies at (513) 772-7000, so that our address record can be corrected. Any information included in this email is provided on an "as is" and "where as" basis, and TSS Technologies makes no representations or warranties of any kind with respect to the completeness or accuracy of the information contained in this email. From scott.marcus at tsstech.com Thu Oct 8 11:16:37 2015 From: scott.marcus at tsstech.com (Scott Marcus) Date: Thu, 8 Oct 2015 12:16:37 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: Here are the flow control TSQL statements... https://msdn.microsoft.com/en-us/library/ms174290.aspx -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of Scott Marcus Sent: Thursday, October 8, 2015 12:14 PM To: 'Discussion concerning MS SQL Server' Subject: Re: [dba-SQLServer] Stored Procedure help John, IF @variable = 'First' BEGIN SELECT * FROM cteMoveNewOld END ELSE IF @variable = 'Second' BEGIN SELECT * FROM cteMoveNewOldWithRowNum END ELSE IF... You get the picture -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of John Colby Sent: Thursday, October 8, 2015 11:51 AM To: Discussion concerning MS SQL Server Subject: [dba-SQLServer] Stored Procedure help I have a stored procedure where I built up a bunch of common table expressions to do various things related to counting data. At the bottom I have a bunch of Select statements which I would like to select ONE from by passing in a parameter. ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), @ToYrMo as char(6) A bunch of CTEs here --SELECT * FROM cteMoveNewOld --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast --SELECT * from cteMoveLastWithDistance SELECT * FROM cteMoveLastDistance_GrpBySt_DistanceCode --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode --Select * from cteMoveLastDistance_GrpByStNew_StOld My current method is to come in and uncomment one line, run the SP, comment that out, uncomment another, run that. So I want to add a new input variable to pass in a report name and use that to select one of the CTEs. ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), @FromYrMo as char(6), @ToYrMo as char(6) AFAICT the ONLY thing I can do as the last line of the SP is a select, which is why I currently uncomment one of the lines to run it. Is there any way to use an IF as the last line (block) to select one of the selects to return data? -- John W. Colby Colby Consulting _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com NOTICE: This electronic mail transmission is for the use of the named individual or entity to which it is directed and may contain information that is privileged or confidential. If you are not the intended recipient, any disclosure, copying, distribution or use of the contents of any information contained herein is prohibited. If you have received this electronic mail transmission in error, delete it from your system without copying or forwarding it, and notify the sender of the error by replying via email or calling TSS Technologies at (513) 772-7000, so that our address record can be corrected. Any information included in this email is provided on an "as is" and "where as" basis, and TSS Technologies makes no representations or warranties of any kind with respect to the completeness or accuracy of the information contained in this email. _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From jwcolby at gmail.com Thu Oct 8 11:21:21 2015 From: jwcolby at gmail.com (John Colby) Date: Thu, 8 Oct 2015 12:21:21 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: Yes but this doesn't work at the bottom of my stored procedure, I have already tried it. I have perhaps 10 Common Table Expressions. At the bottom, below all of those CTEs I MUST HAVE a select statement or the compiler complains. The select statement can be anything, it doesn't have to reference one of the CTEs but it has to be a select. On Thu, Oct 8, 2015 at 12:13 PM, Scott Marcus wrote: > John, > > IF @variable = 'First' > BEGIN > SELECT * FROM cteMoveNewOld > END > ELSE IF @variable = 'Second' > BEGIN > SELECT * FROM cteMoveNewOldWithRowNum > END > ELSE IF... > > You get the picture > > -----Original Message----- > From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] > On Behalf Of John Colby > Sent: Thursday, October 8, 2015 11:51 AM > To: Discussion concerning MS SQL Server < > dba-sqlserver at databaseadvisors.com> > Subject: [dba-SQLServer] Stored Procedure help > > I have a stored procedure where I built up a bunch of common table > expressions to do various things related to counting data. > > At the bottom I have a bunch of Select statements which I would like to > select ONE from by passing in a parameter. > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), @ToYrMo as > char(6) > > A bunch of CTEs here > > --SELECT * FROM cteMoveNewOld > --SELECT * FROM cteMoveNewOldWithRowNum > --SELECT * FROM cteMoveLast > --SELECT * from cteMoveLastWithDistance > SELECT * FROM cteMoveLastDistance_GrpBySt_DistanceCode > --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode > --Select * from cteMoveLastDistance_GrpByStNew_StOld > > My current method is to come in and uncomment one line, run the SP, > comment that out, uncomment another, run that. > > > So I want to add a new input variable to pass in a report name and use > that to select one of the CTEs. > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), @FromYrMo > as char(6), @ToYrMo as char(6) > > AFAICT the ONLY thing I can do as the last line of the SP is a select, > which is why I currently uncomment one of the lines to run it. > > Is there any way to use an IF as the last line (block) to select one of > the selects to return data? > > > -- > John W. Colby > Colby Consulting > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > NOTICE: This electronic mail transmission is for the use of the named > individual or entity to which it is directed and may contain information > that is privileged or confidential. If you are not the intended recipient, > any disclosure, copying, distribution or use of the contents of any > information contained herein is prohibited. If you have received this > electronic mail transmission in error, delete it from your system without > copying or forwarding it, and notify the sender of the error by replying > via email or calling TSS Technologies at (513) 772-7000, so that our > address record can be corrected. > Any information included in this email is provided on an "as is" and > "where as" basis, and TSS Technologies makes no representations or > warranties of any kind with respect to the completeness or accuracy of the > information contained in this email. > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- John W. Colby Colby Consulting From scott.marcus at tsstech.com Thu Oct 8 11:26:05 2015 From: scott.marcus at tsstech.com (Scott Marcus) Date: Thu, 8 Oct 2015 12:26:05 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: You could store the SQL statement in a variable and then execute the stored value outside the if statement. Here is the link for that... https://msdn.microsoft.com/en-us/library/ms188332.aspx -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of John Colby Sent: Thursday, October 8, 2015 12:21 PM To: Discussion concerning MS SQL Server Subject: Re: [dba-SQLServer] Stored Procedure help Yes but this doesn't work at the bottom of my stored procedure, I have already tried it. I have perhaps 10 Common Table Expressions. At the bottom, below all of those CTEs I MUST HAVE a select statement or the compiler complains. The select statement can be anything, it doesn't have to reference one of the CTEs but it has to be a select. On Thu, Oct 8, 2015 at 12:13 PM, Scott Marcus wrote: > John, > > IF @variable = 'First' > BEGIN > SELECT * FROM cteMoveNewOld > END > ELSE IF @variable = 'Second' > BEGIN > SELECT * FROM cteMoveNewOldWithRowNum END ELSE IF... > > You get the picture > > -----Original Message----- > From: dba-SQLServer > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > On Behalf Of John Colby > Sent: Thursday, October 8, 2015 11:51 AM > To: Discussion concerning MS SQL Server < > dba-sqlserver at databaseadvisors.com> > Subject: [dba-SQLServer] Stored Procedure help > > I have a stored procedure where I built up a bunch of common table > expressions to do various things related to counting data. > > At the bottom I have a bunch of Select statements which I would like > to select ONE from by passing in a parameter. > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), > @ToYrMo as > char(6) > > A bunch of CTEs here > > --SELECT * FROM cteMoveNewOld > --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast > --SELECT * from cteMoveLastWithDistance SELECT * FROM > cteMoveLastDistance_GrpBySt_DistanceCode > --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode > --Select * from cteMoveLastDistance_GrpByStNew_StOld > > My current method is to come in and uncomment one line, run the SP, > comment that out, uncomment another, run that. > > > So I want to add a new input variable to pass in a report name and use > that to select one of the CTEs. > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), > @FromYrMo as char(6), @ToYrMo as char(6) > > AFAICT the ONLY thing I can do as the last line of the SP is a select, > which is why I currently uncomment one of the lines to run it. > > Is there any way to use an IF as the last line (block) to select one > of the selects to return data? > > > -- > John W. Colby > Colby Consulting > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > NOTICE: This electronic mail transmission is for the use of the named > individual or entity to which it is directed and may contain > information that is privileged or confidential. If you are not the > intended recipient, any disclosure, copying, distribution or use of > the contents of any information contained herein is prohibited. If > you have received this electronic mail transmission in error, delete > it from your system without copying or forwarding it, and notify the > sender of the error by replying via email or calling TSS Technologies > at (513) 772-7000, so that our address record can be corrected. > Any information included in this email is provided on an "as is" and > "where as" basis, and TSS Technologies makes no representations or > warranties of any kind with respect to the completeness or accuracy of > the information contained in this email. > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- John W. Colby Colby Consulting _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From jwcolby at gmail.com Thu Oct 8 11:58:08 2015 From: jwcolby at gmail.com (John Colby) Date: Thu, 8 Oct 2015 12:58:08 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: I tried that as well. An EXEC is not a Select and cannot be placed at the bottom of the SP. The compiler will complain. ONLY A SELECT can be the last line AFAICT. I thought I would do a "select * from @RptName" where I passed in the name of the CTE. That failed (compile errors). This one is strange because I can pass in the fully qualified path (SELECT * FROM DBName.DBI.ViewName) to an Exec(@SQL) and it will execute and return results, I created a dynamic SQL statement and tried to EXEC(@SQL) and that failed (Compile errors). On Thu, Oct 8, 2015 at 12:26 PM, Scott Marcus wrote: > You could store the SQL statement in a variable and then execute the > stored value outside the if statement. > > Here is the link for that... > > https://msdn.microsoft.com/en-us/library/ms188332.aspx > > -----Original Message----- > From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] > On Behalf Of John Colby > Sent: Thursday, October 8, 2015 12:21 PM > To: Discussion concerning MS SQL Server < > dba-sqlserver at databaseadvisors.com> > Subject: Re: [dba-SQLServer] Stored Procedure help > > Yes but this doesn't work at the bottom of my stored procedure, I have > already tried it. > > I have perhaps 10 Common Table Expressions. At the bottom, below all of > those CTEs I MUST HAVE a select statement or the compiler complains. > > The select statement can be anything, it doesn't have to reference one of > the CTEs but it has to be a select. > > On Thu, Oct 8, 2015 at 12:13 PM, Scott Marcus > wrote: > > > John, > > > > IF @variable = 'First' > > BEGIN > > SELECT * FROM cteMoveNewOld > > END > > ELSE IF @variable = 'Second' > > BEGIN > > SELECT * FROM cteMoveNewOldWithRowNum END ELSE IF... > > > > You get the picture > > > > -----Original Message----- > > From: dba-SQLServer > > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > > On Behalf Of John Colby > > Sent: Thursday, October 8, 2015 11:51 AM > > To: Discussion concerning MS SQL Server < > > dba-sqlserver at databaseadvisors.com> > > Subject: [dba-SQLServer] Stored Procedure help > > > > I have a stored procedure where I built up a bunch of common table > > expressions to do various things related to counting data. > > > > At the bottom I have a bunch of Select statements which I would like > > to select ONE from by passing in a parameter. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), > > @ToYrMo as > > char(6) > > > > A bunch of CTEs here > > > > --SELECT * FROM cteMoveNewOld > > --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast > > --SELECT * from cteMoveLastWithDistance SELECT * FROM > > cteMoveLastDistance_GrpBySt_DistanceCode > > --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode > > --Select * from cteMoveLastDistance_GrpByStNew_StOld > > > > My current method is to come in and uncomment one line, run the SP, > > comment that out, uncomment another, run that. > > > > > > So I want to add a new input variable to pass in a report name and use > > that to select one of the CTEs. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), > > @FromYrMo as char(6), @ToYrMo as char(6) > > > > AFAICT the ONLY thing I can do as the last line of the SP is a select, > > which is why I currently uncomment one of the lines to run it. > > > > Is there any way to use an IF as the last line (block) to select one > > of the selects to return data? > > > > > > -- > > John W. Colby > > Colby Consulting > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > NOTICE: This electronic mail transmission is for the use of the named > > individual or entity to which it is directed and may contain > > information that is privileged or confidential. If you are not the > > intended recipient, any disclosure, copying, distribution or use of > > the contents of any information contained herein is prohibited. If > > you have received this electronic mail transmission in error, delete > > it from your system without copying or forwarding it, and notify the > > sender of the error by replying via email or calling TSS Technologies > > at (513) 772-7000, so that our address record can be corrected. > > Any information included in this email is provided on an "as is" and > > "where as" basis, and TSS Technologies makes no representations or > > warranties of any kind with respect to the completeness or accuracy of > > the information contained in this email. > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > > -- > John W. Colby > Colby Consulting > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- John W. Colby Colby Consulting From scott.marcus at tsstech.com Thu Oct 8 12:01:19 2015 From: scott.marcus at tsstech.com (Scott Marcus) Date: Thu, 8 Oct 2015 13:01:19 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: How about... SELECT * from EXEC(@SQL) -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of John Colby Sent: Thursday, October 8, 2015 12:58 PM To: Discussion concerning MS SQL Server Subject: Re: [dba-SQLServer] Stored Procedure help I tried that as well. An EXEC is not a Select and cannot be placed at the bottom of the SP. The compiler will complain. ONLY A SELECT can be the last line AFAICT. I thought I would do a "select * from @RptName" where I passed in the name of the CTE. That failed (compile errors). This one is strange because I can pass in the fully qualified path (SELECT * FROM DBName.DBI.ViewName) to an Exec(@SQL) and it will execute and return results, I created a dynamic SQL statement and tried to EXEC(@SQL) and that failed (Compile errors). On Thu, Oct 8, 2015 at 12:26 PM, Scott Marcus wrote: > You could store the SQL statement in a variable and then execute the > stored value outside the if statement. > > Here is the link for that... > > https://msdn.microsoft.com/en-us/library/ms188332.aspx > > -----Original Message----- > From: dba-SQLServer > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > On Behalf Of John Colby > Sent: Thursday, October 8, 2015 12:21 PM > To: Discussion concerning MS SQL Server < > dba-sqlserver at databaseadvisors.com> > Subject: Re: [dba-SQLServer] Stored Procedure help > > Yes but this doesn't work at the bottom of my stored procedure, I have > already tried it. > > I have perhaps 10 Common Table Expressions. At the bottom, below all > of those CTEs I MUST HAVE a select statement or the compiler complains. > > The select statement can be anything, it doesn't have to reference one > of the CTEs but it has to be a select. > > On Thu, Oct 8, 2015 at 12:13 PM, Scott Marcus > > wrote: > > > John, > > > > IF @variable = 'First' > > BEGIN > > SELECT * FROM cteMoveNewOld > > END > > ELSE IF @variable = 'Second' > > BEGIN > > SELECT * FROM cteMoveNewOldWithRowNum END ELSE IF... > > > > You get the picture > > > > -----Original Message----- > > From: dba-SQLServer > > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > > On Behalf Of John Colby > > Sent: Thursday, October 8, 2015 11:51 AM > > To: Discussion concerning MS SQL Server < > > dba-sqlserver at databaseadvisors.com> > > Subject: [dba-SQLServer] Stored Procedure help > > > > I have a stored procedure where I built up a bunch of common table > > expressions to do various things related to counting data. > > > > At the bottom I have a bunch of Select statements which I would like > > to select ONE from by passing in a parameter. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), > > @ToYrMo as > > char(6) > > > > A bunch of CTEs here > > > > --SELECT * FROM cteMoveNewOld > > --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast > > --SELECT * from cteMoveLastWithDistance SELECT * FROM > > cteMoveLastDistance_GrpBySt_DistanceCode > > --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode > > --Select * from cteMoveLastDistance_GrpByStNew_StOld > > > > My current method is to come in and uncomment one line, run the SP, > > comment that out, uncomment another, run that. > > > > > > So I want to add a new input variable to pass in a report name and > > use that to select one of the CTEs. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), > > @FromYrMo as char(6), @ToYrMo as char(6) > > > > AFAICT the ONLY thing I can do as the last line of the SP is a > > select, which is why I currently uncomment one of the lines to run it. > > > > Is there any way to use an IF as the last line (block) to select one > > of the selects to return data? > > > > > > -- > > John W. Colby > > Colby Consulting > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > NOTICE: This electronic mail transmission is for the use of the > > named individual or entity to which it is directed and may contain > > information that is privileged or confidential. If you are not the > > intended recipient, any disclosure, copying, distribution or use of > > the contents of any information contained herein is prohibited. If > > you have received this electronic mail transmission in error, delete > > it from your system without copying or forwarding it, and notify the > > sender of the error by replying via email or calling TSS > > Technologies at (513) 772-7000, so that our address record can be corrected. > > Any information included in this email is provided on an "as is" and > > "where as" basis, and TSS Technologies makes no representations or > > warranties of any kind with respect to the completeness or accuracy > > of the information contained in this email. > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > > -- > John W. Colby > Colby Consulting > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- John W. Colby Colby Consulting _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From paul.hartland at googlemail.com Thu Oct 8 12:01:35 2015 From: paul.hartland at googlemail.com (Paul Hartland) Date: Thu, 8 Oct 2015 18:01:35 +0100 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: I hadn't forgot about this (if it's the same question you asked a while back) but can't see any way to do it, apart from changing your CTE's into #Temp tables instead. On 8 October 2015 at 17:58, John Colby wrote: > I tried that as well. An EXEC is not a Select and cannot be placed at the > bottom of the SP. The compiler will complain. > > ONLY A SELECT can be the last line AFAICT. > > I thought I would do a "select * from @RptName" where I passed in the name > of the CTE. That failed (compile errors). This one is strange because I > can pass in the fully qualified path (SELECT * FROM DBName.DBI.ViewName) to > an Exec(@SQL) and it will execute and return results, > > I created a dynamic SQL statement and tried to EXEC(@SQL) and that failed > (Compile errors). > > > > On Thu, Oct 8, 2015 at 12:26 PM, Scott Marcus > wrote: > > > You could store the SQL statement in a variable and then execute the > > stored value outside the if statement. > > > > Here is the link for that... > > > > https://msdn.microsoft.com/en-us/library/ms188332.aspx > > > > -----Original Message----- > > From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] > > On Behalf Of John Colby > > Sent: Thursday, October 8, 2015 12:21 PM > > To: Discussion concerning MS SQL Server < > > dba-sqlserver at databaseadvisors.com> > > Subject: Re: [dba-SQLServer] Stored Procedure help > > > > Yes but this doesn't work at the bottom of my stored procedure, I have > > already tried it. > > > > I have perhaps 10 Common Table Expressions. At the bottom, below all of > > those CTEs I MUST HAVE a select statement or the compiler complains. > > > > The select statement can be anything, it doesn't have to reference one of > > the CTEs but it has to be a select. > > > > On Thu, Oct 8, 2015 at 12:13 PM, Scott Marcus > > wrote: > > > > > John, > > > > > > IF @variable = 'First' > > > BEGIN > > > SELECT * FROM cteMoveNewOld > > > END > > > ELSE IF @variable = 'Second' > > > BEGIN > > > SELECT * FROM cteMoveNewOldWithRowNum END ELSE IF... > > > > > > You get the picture > > > > > > -----Original Message----- > > > From: dba-SQLServer > > > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > > > On Behalf Of John Colby > > > Sent: Thursday, October 8, 2015 11:51 AM > > > To: Discussion concerning MS SQL Server < > > > dba-sqlserver at databaseadvisors.com> > > > Subject: [dba-SQLServer] Stored Procedure help > > > > > > I have a stored procedure where I built up a bunch of common table > > > expressions to do various things related to counting data. > > > > > > At the bottom I have a bunch of Select statements which I would like > > > to select ONE from by passing in a parameter. > > > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), > > > @ToYrMo as > > > char(6) > > > > > > A bunch of CTEs here > > > > > > --SELECT * FROM cteMoveNewOld > > > --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast > > > --SELECT * from cteMoveLastWithDistance SELECT * FROM > > > cteMoveLastDistance_GrpBySt_DistanceCode > > > --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode > > > --Select * from cteMoveLastDistance_GrpByStNew_StOld > > > > > > My current method is to come in and uncomment one line, run the SP, > > > comment that out, uncomment another, run that. > > > > > > > > > So I want to add a new input variable to pass in a report name and use > > > that to select one of the CTEs. > > > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), > > > @FromYrMo as char(6), @ToYrMo as char(6) > > > > > > AFAICT the ONLY thing I can do as the last line of the SP is a select, > > > which is why I currently uncomment one of the lines to run it. > > > > > > Is there any way to use an IF as the last line (block) to select one > > > of the selects to return data? > > > > > > > > > -- > > > John W. Colby > > > Colby Consulting > > > _______________________________________________ > > > dba-SQLServer mailing list > > > dba-SQLServer at databaseadvisors.com > > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > > http://www.databaseadvisors.com > > > > > > > > > NOTICE: This electronic mail transmission is for the use of the named > > > individual or entity to which it is directed and may contain > > > information that is privileged or confidential. If you are not the > > > intended recipient, any disclosure, copying, distribution or use of > > > the contents of any information contained herein is prohibited. If > > > you have received this electronic mail transmission in error, delete > > > it from your system without copying or forwarding it, and notify the > > > sender of the error by replying via email or calling TSS Technologies > > > at (513) 772-7000, so that our address record can be corrected. > > > Any information included in this email is provided on an "as is" and > > > "where as" basis, and TSS Technologies makes no representations or > > > warranties of any kind with respect to the completeness or accuracy of > > > the information contained in this email. > > > > > > _______________________________________________ > > > dba-SQLServer mailing list > > > dba-SQLServer at databaseadvisors.com > > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > > http://www.databaseadvisors.com > > > > > > > > > > > > -- > > John W. Colby > > Colby Consulting > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > > -- > John W. Colby > Colby Consulting > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- Paul Hartland paul.hartland at googlemail.com From scott.marcus at tsstech.com Thu Oct 8 12:06:50 2015 From: scott.marcus at tsstech.com (Scott Marcus) Date: Thu, 8 Oct 2015 13:06:50 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: John, I have lots of stored procedures that do not end with a select statement. Please include your procedure so that we can have a look at what you are doing. Scott Marcus -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of John Colby Sent: Thursday, October 8, 2015 12:58 PM To: Discussion concerning MS SQL Server Subject: Re: [dba-SQLServer] Stored Procedure help I tried that as well. An EXEC is not a Select and cannot be placed at the bottom of the SP. The compiler will complain. ONLY A SELECT can be the last line AFAICT. I thought I would do a "select * from @RptName" where I passed in the name of the CTE. That failed (compile errors). This one is strange because I can pass in the fully qualified path (SELECT * FROM DBName.DBI.ViewName) to an Exec(@SQL) and it will execute and return results, I created a dynamic SQL statement and tried to EXEC(@SQL) and that failed (Compile errors). On Thu, Oct 8, 2015 at 12:26 PM, Scott Marcus wrote: > You could store the SQL statement in a variable and then execute the > stored value outside the if statement. > > Here is the link for that... > > https://msdn.microsoft.com/en-us/library/ms188332.aspx > > -----Original Message----- > From: dba-SQLServer > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > On Behalf Of John Colby > Sent: Thursday, October 8, 2015 12:21 PM > To: Discussion concerning MS SQL Server < > dba-sqlserver at databaseadvisors.com> > Subject: Re: [dba-SQLServer] Stored Procedure help > > Yes but this doesn't work at the bottom of my stored procedure, I have > already tried it. > > I have perhaps 10 Common Table Expressions. At the bottom, below all > of those CTEs I MUST HAVE a select statement or the compiler complains. > > The select statement can be anything, it doesn't have to reference one > of the CTEs but it has to be a select. > > On Thu, Oct 8, 2015 at 12:13 PM, Scott Marcus > > wrote: > > > John, > > > > IF @variable = 'First' > > BEGIN > > SELECT * FROM cteMoveNewOld > > END > > ELSE IF @variable = 'Second' > > BEGIN > > SELECT * FROM cteMoveNewOldWithRowNum END ELSE IF... > > > > You get the picture > > > > -----Original Message----- > > From: dba-SQLServer > > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > > On Behalf Of John Colby > > Sent: Thursday, October 8, 2015 11:51 AM > > To: Discussion concerning MS SQL Server < > > dba-sqlserver at databaseadvisors.com> > > Subject: [dba-SQLServer] Stored Procedure help > > > > I have a stored procedure where I built up a bunch of common table > > expressions to do various things related to counting data. > > > > At the bottom I have a bunch of Select statements which I would like > > to select ONE from by passing in a parameter. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), > > @ToYrMo as > > char(6) > > > > A bunch of CTEs here > > > > --SELECT * FROM cteMoveNewOld > > --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast > > --SELECT * from cteMoveLastWithDistance SELECT * FROM > > cteMoveLastDistance_GrpBySt_DistanceCode > > --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode > > --Select * from cteMoveLastDistance_GrpByStNew_StOld > > > > My current method is to come in and uncomment one line, run the SP, > > comment that out, uncomment another, run that. > > > > > > So I want to add a new input variable to pass in a report name and > > use that to select one of the CTEs. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), > > @FromYrMo as char(6), @ToYrMo as char(6) > > > > AFAICT the ONLY thing I can do as the last line of the SP is a > > select, which is why I currently uncomment one of the lines to run it. > > > > Is there any way to use an IF as the last line (block) to select one > > of the selects to return data? > > > > > > -- > > John W. Colby > > Colby Consulting > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > NOTICE: This electronic mail transmission is for the use of the > > named individual or entity to which it is directed and may contain > > information that is privileged or confidential. If you are not the > > intended recipient, any disclosure, copying, distribution or use of > > the contents of any information contained herein is prohibited. If > > you have received this electronic mail transmission in error, delete > > it from your system without copying or forwarding it, and notify the > > sender of the error by replying via email or calling TSS > > Technologies at (513) 772-7000, so that our address record can be corrected. > > Any information included in this email is provided on an "as is" and > > "where as" basis, and TSS Technologies makes no representations or > > warranties of any kind with respect to the completeness or accuracy > > of the information contained in this email. > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > > -- > John W. Colby > Colby Consulting > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- John W. Colby Colby Consulting _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From scott.marcus at tsstech.com Thu Oct 8 12:11:46 2015 From: scott.marcus at tsstech.com (Scott Marcus) Date: Thu, 8 Oct 2015 13:11:46 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: John, I'm sorry. I see that you did include the procedure. Scott Marcus -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of Scott Marcus Sent: Thursday, October 8, 2015 1:07 PM To: 'Discussion concerning MS SQL Server' Subject: Re: [dba-SQLServer] Stored Procedure help John, I have lots of stored procedures that do not end with a select statement. Please include your procedure so that we can have a look at what you are doing. Scott Marcus -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of John Colby Sent: Thursday, October 8, 2015 12:58 PM To: Discussion concerning MS SQL Server Subject: Re: [dba-SQLServer] Stored Procedure help I tried that as well. An EXEC is not a Select and cannot be placed at the bottom of the SP. The compiler will complain. ONLY A SELECT can be the last line AFAICT. I thought I would do a "select * from @RptName" where I passed in the name of the CTE. That failed (compile errors). This one is strange because I can pass in the fully qualified path (SELECT * FROM DBName.DBI.ViewName) to an Exec(@SQL) and it will execute and return results, I created a dynamic SQL statement and tried to EXEC(@SQL) and that failed (Compile errors). On Thu, Oct 8, 2015 at 12:26 PM, Scott Marcus wrote: > You could store the SQL statement in a variable and then execute the > stored value outside the if statement. > > Here is the link for that... > > https://msdn.microsoft.com/en-us/library/ms188332.aspx > > -----Original Message----- > From: dba-SQLServer > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > On Behalf Of John Colby > Sent: Thursday, October 8, 2015 12:21 PM > To: Discussion concerning MS SQL Server < > dba-sqlserver at databaseadvisors.com> > Subject: Re: [dba-SQLServer] Stored Procedure help > > Yes but this doesn't work at the bottom of my stored procedure, I have > already tried it. > > I have perhaps 10 Common Table Expressions. At the bottom, below all > of those CTEs I MUST HAVE a select statement or the compiler complains. > > The select statement can be anything, it doesn't have to reference one > of the CTEs but it has to be a select. > > On Thu, Oct 8, 2015 at 12:13 PM, Scott Marcus > > wrote: > > > John, > > > > IF @variable = 'First' > > BEGIN > > SELECT * FROM cteMoveNewOld > > END > > ELSE IF @variable = 'Second' > > BEGIN > > SELECT * FROM cteMoveNewOldWithRowNum END ELSE IF... > > > > You get the picture > > > > -----Original Message----- > > From: dba-SQLServer > > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > > On Behalf Of John Colby > > Sent: Thursday, October 8, 2015 11:51 AM > > To: Discussion concerning MS SQL Server < > > dba-sqlserver at databaseadvisors.com> > > Subject: [dba-SQLServer] Stored Procedure help > > > > I have a stored procedure where I built up a bunch of common table > > expressions to do various things related to counting data. > > > > At the bottom I have a bunch of Select statements which I would like > > to select ONE from by passing in a parameter. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), > > @ToYrMo as > > char(6) > > > > A bunch of CTEs here > > > > --SELECT * FROM cteMoveNewOld > > --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast > > --SELECT * from cteMoveLastWithDistance SELECT * FROM > > cteMoveLastDistance_GrpBySt_DistanceCode > > --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode > > --Select * from cteMoveLastDistance_GrpByStNew_StOld > > > > My current method is to come in and uncomment one line, run the SP, > > comment that out, uncomment another, run that. > > > > > > So I want to add a new input variable to pass in a report name and > > use that to select one of the CTEs. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), > > @FromYrMo as char(6), @ToYrMo as char(6) > > > > AFAICT the ONLY thing I can do as the last line of the SP is a > > select, which is why I currently uncomment one of the lines to run it. > > > > Is there any way to use an IF as the last line (block) to select one > > of the selects to return data? > > > > > > -- > > John W. Colby > > Colby Consulting > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > NOTICE: This electronic mail transmission is for the use of the > > named individual or entity to which it is directed and may contain > > information that is privileged or confidential. If you are not the > > intended recipient, any disclosure, copying, distribution or use of > > the contents of any information contained herein is prohibited. If > > you have received this electronic mail transmission in error, delete > > it from your system without copying or forwarding it, and notify the > > sender of the error by replying via email or calling TSS > > Technologies at (513) 772-7000, so that our address record can be corrected. > > Any information included in this email is provided on an "as is" and > > "where as" basis, and TSS Technologies makes no representations or > > warranties of any kind with respect to the completeness or accuracy > > of the information contained in this email. > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > > -- > John W. Colby > Colby Consulting > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- John W. Colby Colby Consulting _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From scott.marcus at tsstech.com Thu Oct 8 12:18:25 2015 From: scott.marcus at tsstech.com (Scott Marcus) Date: Thu, 8 Oct 2015 13:18:25 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: Message-ID: John, I do not see any begin or end statements in the procedure you included (or a properly formatted parameter list). Did you give us a complete copy of your procedure? Thanks, Scott Marcus -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of Scott Marcus Sent: Thursday, October 8, 2015 1:12 PM To: 'Discussion concerning MS SQL Server' Subject: Re: [dba-SQLServer] Stored Procedure help John, I'm sorry. I see that you did include the procedure. Scott Marcus -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of Scott Marcus Sent: Thursday, October 8, 2015 1:07 PM To: 'Discussion concerning MS SQL Server' Subject: Re: [dba-SQLServer] Stored Procedure help John, I have lots of stored procedures that do not end with a select statement. Please include your procedure so that we can have a look at what you are doing. Scott Marcus -----Original Message----- From: dba-SQLServer [mailto:dba-sqlserver-bounces at databaseadvisors.com] On Behalf Of John Colby Sent: Thursday, October 8, 2015 12:58 PM To: Discussion concerning MS SQL Server Subject: Re: [dba-SQLServer] Stored Procedure help I tried that as well. An EXEC is not a Select and cannot be placed at the bottom of the SP. The compiler will complain. ONLY A SELECT can be the last line AFAICT. I thought I would do a "select * from @RptName" where I passed in the name of the CTE. That failed (compile errors). This one is strange because I can pass in the fully qualified path (SELECT * FROM DBName.DBI.ViewName) to an Exec(@SQL) and it will execute and return results, I created a dynamic SQL statement and tried to EXEC(@SQL) and that failed (Compile errors). On Thu, Oct 8, 2015 at 12:26 PM, Scott Marcus wrote: > You could store the SQL statement in a variable and then execute the > stored value outside the if statement. > > Here is the link for that... > > https://msdn.microsoft.com/en-us/library/ms188332.aspx > > -----Original Message----- > From: dba-SQLServer > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > On Behalf Of John Colby > Sent: Thursday, October 8, 2015 12:21 PM > To: Discussion concerning MS SQL Server < > dba-sqlserver at databaseadvisors.com> > Subject: Re: [dba-SQLServer] Stored Procedure help > > Yes but this doesn't work at the bottom of my stored procedure, I have > already tried it. > > I have perhaps 10 Common Table Expressions. At the bottom, below all > of those CTEs I MUST HAVE a select statement or the compiler complains. > > The select statement can be anything, it doesn't have to reference one > of the CTEs but it has to be a select. > > On Thu, Oct 8, 2015 at 12:13 PM, Scott Marcus > > wrote: > > > John, > > > > IF @variable = 'First' > > BEGIN > > SELECT * FROM cteMoveNewOld > > END > > ELSE IF @variable = 'Second' > > BEGIN > > SELECT * FROM cteMoveNewOldWithRowNum END ELSE IF... > > > > You get the picture > > > > -----Original Message----- > > From: dba-SQLServer > > [mailto:dba-sqlserver-bounces at databaseadvisors.com] > > On Behalf Of John Colby > > Sent: Thursday, October 8, 2015 11:51 AM > > To: Discussion concerning MS SQL Server < > > dba-sqlserver at databaseadvisors.com> > > Subject: [dba-SQLServer] Stored Procedure help > > > > I have a stored procedure where I built up a bunch of common table > > expressions to do various things related to counting data. > > > > At the bottom I have a bunch of Select statements which I would like > > to select ONE from by passing in a parameter. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @FromYrMo as char(6), > > @ToYrMo as > > char(6) > > > > A bunch of CTEs here > > > > --SELECT * FROM cteMoveNewOld > > --SELECT * FROM cteMoveNewOldWithRowNum --SELECT * FROM cteMoveLast > > --SELECT * from cteMoveLastWithDistance SELECT * FROM > > cteMoveLastDistance_GrpBySt_DistanceCode > > --SELECT * FROM cteMoveLastDistance_GrpByMSA_DistanceCode > > --Select * from cteMoveLastDistance_GrpByStNew_StOld > > > > My current method is to come in and uncomment one line, run the SP, > > comment that out, uncomment another, run that. > > > > > > So I want to add a new input variable to pass in a report name and > > use that to select one of the CTEs. > > > > ALTER PROCEDURE [dbo].[usp_MoveDistance] @RptName varchar(50), > > @FromYrMo as char(6), @ToYrMo as char(6) > > > > AFAICT the ONLY thing I can do as the last line of the SP is a > > select, which is why I currently uncomment one of the lines to run it. > > > > Is there any way to use an IF as the last line (block) to select one > > of the selects to return data? > > > > > > -- > > John W. Colby > > Colby Consulting > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > NOTICE: This electronic mail transmission is for the use of the > > named individual or entity to which it is directed and may contain > > information that is privileged or confidential. If you are not the > > intended recipient, any disclosure, copying, distribution or use of > > the contents of any information contained herein is prohibited. If > > you have received this electronic mail transmission in error, delete > > it from your system without copying or forwarding it, and notify the > > sender of the error by replying via email or calling TSS > > Technologies at (513) 772-7000, so that our address record can be corrected. > > Any information included in this email is provided on an "as is" and > > "where as" basis, and TSS Technologies makes no representations or > > warranties of any kind with respect to the completeness or accuracy > > of the information contained in this email. > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > > -- > John W. Colby > Colby Consulting > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- John W. Colby Colby Consulting _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com _______________________________________________ dba-SQLServer mailing list dba-SQLServer at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/dba-sqlserver http://www.databaseadvisors.com From stuart at lexacorp.com.pg Thu Oct 8 16:48:44 2015 From: stuart at lexacorp.com.pg (Stuart McLachlan) Date: Fri, 09 Oct 2015 07:48:44 +1000 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: References: , , Message-ID: <5616E4BC.26184.7DE76BE@stuart.lexacorp.com.pg> That doesn't make sense.? You don't have to have a select in a stored procedure at all. This works fine for me: CREATE PROCEDURE test2 AS BEGIN RETURN 1 END GO But if you really want a select, this works just as well: CREATE PROCEDURE test3 AS BEGIN SELECT 1 END GO On 8 Oct 2015 at 12:21, John Colby wrote: > Yes but this doesn't work at the bottom of my stored procedure, I have > already tried it. > > I have perhaps 10 Common Table Expressions.? At the bottom, below all > of those CTEs I MUST HAVE a select statement or the compiler > complains. > > The select statement can be anything, it doesn't have to reference one > of the CTEs but it has to be a select. > From stuart at lexacorp.com.pg Thu Oct 8 18:39:24 2015 From: stuart at lexacorp.com.pg (Stuart McLachlan) Date: Fri, 09 Oct 2015 09:39:24 +1000 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: <5616E4BC.26184.7DE76BE@stuart.lexacorp.com.pg> References: , , <5616E4BC.26184.7DE76BE@stuart.lexacorp.com.pg> Message-ID: <5616FEAC.24591.843C646@stuart.lexacorp.com.pg> Ahah - missed the fact that it is the use of CTEs that is casuing the problem. It would have helped if you had told us the error message when trying to create the SP. I presume it is "Common table expression defined but not used." Yep, that's a requirement of CTEs. If you define them, you must use them in some sort of query after the definition. Sure you can fool SQL Server into thinking you have used it with something like "SELECT 1", but that is not optimal. -- Stuart On 9 Oct 2015 at 7:48, Stuart McLachlan wrote: > > That doesn't make sense.? You don't have to have a select in a stored > procedure at all. > > This works fine for me: > > CREATE PROCEDURE test2 > AS > BEGIN > RETURN 1 > END > GO > > > But if you really want a select, this works just as well: > > CREATE PROCEDURE test3 > AS > BEGIN > SELECT 1 > END > GO > > > > On 8 Oct 2015 at 12:21, John Colby wrote: > > > Yes but this doesn't work at the bottom of my stored procedure, I > > have already tried it. > > > > I have perhaps 10 Common Table Expressions.? At the bottom, below > > all of those CTEs I MUST HAVE a select statement or the compiler > > complains. > > > > The select statement can be anything, it doesn't have to reference > > one of the CTEs but it has to be a select. > > > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > From jwcolby at gmail.com Thu Oct 8 20:34:23 2015 From: jwcolby at gmail.com (John Colby) Date: Thu, 8 Oct 2015 21:34:23 -0400 Subject: [dba-SQLServer] Stored Procedure help In-Reply-To: <5616FEAC.24591.843C646@stuart.lexacorp.com.pg> References: <5616E4BC.26184.7DE76BE@stuart.lexacorp.com.pg> <5616FEAC.24591.843C646@stuart.lexacorp.com.pg> Message-ID: And if you do "fool it" then all the CTEs defined are no longer valid after that. I am building up a report from a set of two tables, addresses and old addresses. The objective is to find moves, then if any exist the "old address". Having found the old address(es) then I have to select the most recent "old address" (there may be several). From that I have to compute a distance between the latest address and the most recent old address. Having THAT, I have to count the move records grouped by MSA, State, State old / State new, distance code etc. So there is a ton of stuff "building up a base data set, then a set of several queries which simply take that data and count it grouping by different things. To make matters more difficult I have to pass in parameters for which months (move year/month) I want to perform these reports over. CTEs make this all somewhat manageable since I can pass in the parameters and build the CTEs to select the correct set of move records, then build up on those to join to the old move table, perform an Over to select the latest of the old move records for each new move record, then build a CTE for each final report. I have to rethink this whole thing. Perhaps have a base SP which builds up the base data, then another SP which takes the ReportName parameter, calls the first SP to get the basic data, and using that grab the correct final report data. Or something. On Thu, Oct 8, 2015 at 7:39 PM, Stuart McLachlan wrote: > Ahah - missed the fact that it is the use of CTEs that is casuing the > problem. > > It would have helped if you had told us the error message when trying to > create the SP. > > I presume it is "Common table expression defined but not used." > > Yep, that's a requirement of CTEs. If you define them, you must use them > in some > sort of query after the definition. Sure you can fool SQL Server into > thinking > you have used it with something like "SELECT 1", but that is not optimal. > > -- > Stuart > > > On 9 Oct 2015 at 7:48, Stuart McLachlan wrote: > > > > > That doesn't make sense. You don't have to have a select in a stored > > procedure at all. > > > > This works fine for me: > > > > CREATE PROCEDURE test2 > > AS > > BEGIN > > RETURN 1 > > END > > GO > > > > > > But if you really want a select, this works just as well: > > > > CREATE PROCEDURE test3 > > AS > > BEGIN > > SELECT 1 > > END > > GO > > > > > > > > On 8 Oct 2015 at 12:21, John Colby wrote: > > > > > Yes but this doesn't work at the bottom of my stored procedure, I > > > have already tried it. > > > > > > I have perhaps 10 Common Table Expressions. At the bottom, below > > > all of those CTEs I MUST HAVE a select statement or the compiler > > > complains. > > > > > > The select statement can be anything, it doesn't have to reference > > > one of the CTEs but it has to be a select. > > > > > > > > > _______________________________________________ > > dba-SQLServer mailing list > > dba-SQLServer at databaseadvisors.com > > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > > http://www.databaseadvisors.com > > > > > > > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- John W. Colby Colby Consulting From mcp2004 at mail.ru Fri Oct 16 11:08:54 2015 From: mcp2004 at mail.ru (=?UTF-8?B?U2FsYWtoZXRkaW5vdiBTaGFtaWw=?=) Date: Fri, 16 Oct 2015 19:08:54 +0300 Subject: [dba-SQLServer] =?utf-8?q?Stored_Procedure_help?= In-Reply-To: References: <5616FEAC.24591.843C646@stuart.lexacorp.com.pg> Message-ID: <1445011734.920583653@f412.i.mail.ru> >>>? Perhaps have a base SP which builds up? the base data, John -- I'd use table-valued UDFs here, e.g.?http://tinyurl.com/nlfxcfj See also? http://tinyurl.com/q5pctua ?to get informed what UDFs use cases to avoid and http://tinyurl.com/nkhwrj ?- to find out?what are right for you to go ... Enjoy! :) -- Shamil >Thursday, October 8, 2015 9:34 PM -04:00 from John Colby : > >And if you do "fool it" then all the CTEs defined are no longer valid after >that. > >I am building up a report from a set of two tables, addresses and old >addresses. The objective is to find moves, then if any exist the "old >address". Having found the old address(es) then I have to select the most >recent "old address" (there may be several). From that I have to compute a >distance between the latest address and the most recent old address. >Having THAT, I have to count the move records grouped by MSA, State, State >old / State new, distance code etc. So there is a ton of stuff "building >up a base data set, then a set of several queries which simply take that >data and count it grouping by different things. > >To make matters more difficult I have to pass in parameters for which >months (move year/month) I want to perform these reports over. CTEs make >this all somewhat manageable since I can pass in the parameters and build >the CTEs to select the correct set of move records, then build up on those >to join to the old move table, perform an Over to select the latest of the >old move records for each new move record, then build a CTE for each final >report. > >I have to rethink this whole thing. Perhaps have a base SP which builds up >the base data, then another SP which takes the ReportName parameter, calls >the first SP to get the basic data, and using that grab the correct final >report data. Or something. <<< skipped >>> > From jwcolby at gmail.com Thu Oct 29 11:35:48 2015 From: jwcolby at gmail.com (John Colby) Date: Thu, 29 Oct 2015 12:35:48 -0400 Subject: [dba-SQLServer] Invalid column name dbo. Message-ID: I have a query which is taking hours to complete. IMHO it shouldn't and so I am trying to speed things up by preselecting data in temp tables. I create the table and a unique index on it, then insert the data, about 900K rows. Notice the two select statements which I created for testing how long it took to return data. The first select runs just fine, and finishes selecting the data in about 13 seconds. The second select then joins the temp table to a largish table (7+ billion rows). It likewise returns the data in about the same time, the same number of rows as the first select. The problem is that it presents an error in the messages tab Msg 207, Level 16, State 1, Procedure usp_MovesSimmons, Line 72 Invalid column name 'dbo'. I cannot see where there is a column named .dbo in the second select. To reiterate - No error message if I uncomment the first statement and run just that select. Error if I comment that out and run just the second select. WEIRD! Can anyone see what I may be missing? Create Table #MovePKs (PKMove int, St char(2), Zip char(5), MSA Char (4), MSAPlusName varchar(100)); CREATE UNIQUE CLUSTERED INDEX IX_1 on #MovePKs (PKMove) insert into #MovePKs (PKMove, St, Zip, MSA, MSAPlusName) SELECT _DataDB101.dbo.AZData.pk, _DataDB101.dbo.AZData.st, _DataDB101.dbo.AZData.zip5, ZipCodes.dbo.vZipStSimple.MSA, ZipCodes.dbo.vZipStSimple.MSAPlusName FROM _DataDB101.dbo.AZData INNER JOIN ZipCodes.dbo.vZipStSimple ON _DataDB101.dbo.AZData.zip5 = ZipCodes.dbo.vZipStSimple.Zip WHERE (NOT (_DataDB101.dbo.AZData.HashPerson IS NULL)) AND (_DataDB101.dbo.AZData.AddrValid IN ('V', 'E', 'PO')) AND (_DataDB101.dbo.AZData.movedate_ >= @FromYrMo) AND (_DataDB101.dbo.AZData.movedate_ <= @ToYrMo) --Select St, Zip, MSA, MSAPlusName from #MovePKs Select St, Zip, MSA, MSAPlusName from #MovePKs Inner Join _DataSimmonsProtocolSimple.dbo.tblDB101SimmonsResultSimple ON #MovePKs.dbo.PKMove = _DataSimmonsProtocolSimple.dbo.tblDB101SimmonsResultSimple.PKDb101 -- John W. Colby Colby Consulting From paul.hartland at googlemail.com Thu Oct 29 12:19:20 2015 From: paul.hartland at googlemail.com (Paul Hartland) Date: Thu, 29 Oct 2015 17:19:20 +0000 Subject: [dba-SQLServer] Invalid column name dbo. In-Reply-To: References: Message-ID: John, I am pretty sure it is this line ON #MovePKs.dbo.PKMove Change to ON #MovePKs.PKMove On 29 October 2015 at 16:35, John Colby wrote: > I have a query which is taking hours to complete. IMHO it shouldn't and so > I am trying to speed things up by preselecting data in temp tables. > > I create the table and a unique index on it, then insert the data, about > 900K rows. > > Notice the two select statements which I created for testing how long it > took to return data. The first select runs just fine, and finishes > selecting the data in about 13 seconds. > > The second select then joins the temp table to a largish table (7+ billion > rows). It likewise returns the data in about the same time, the same > number of rows as the first select. The problem is that it presents an > error in the messages tab > > Msg 207, Level 16, State 1, Procedure usp_MovesSimmons, Line 72 > Invalid column name 'dbo'. > > I cannot see where there is a column named .dbo in the second select. > > To reiterate - No error message if I uncomment the first statement and run > just that select. Error if I comment that out and run just the second > select. > > WEIRD! > > Can anyone see what I may be missing? > > Create Table #MovePKs (PKMove int, St char(2), Zip char(5), MSA Char (4), > MSAPlusName varchar(100)); > CREATE UNIQUE CLUSTERED INDEX IX_1 on #MovePKs (PKMove) > > insert into #MovePKs (PKMove, St, Zip, MSA, MSAPlusName) > SELECT _DataDB101.dbo.AZData.pk, > _DataDB101.dbo.AZData.st, > _DataDB101.dbo.AZData.zip5, > ZipCodes.dbo.vZipStSimple.MSA, > ZipCodes.dbo.vZipStSimple.MSAPlusName > FROM _DataDB101.dbo.AZData > INNER JOIN ZipCodes.dbo.vZipStSimple > ON _DataDB101.dbo.AZData.zip5 = ZipCodes.dbo.vZipStSimple.Zip > WHERE (NOT (_DataDB101.dbo.AZData.HashPerson IS NULL)) > AND (_DataDB101.dbo.AZData.AddrValid IN ('V', 'E', 'PO')) > AND (_DataDB101.dbo.AZData.movedate_ >= @FromYrMo) > AND (_DataDB101.dbo.AZData.movedate_ <= @ToYrMo) > > --Select St, Zip, MSA, MSAPlusName from #MovePKs > > Select St, Zip, MSA, MSAPlusName > from #MovePKs > Inner Join _DataSimmonsProtocolSimple.dbo.tblDB101SimmonsResultSimple > ON #MovePKs.dbo.PKMove = > _DataSimmonsProtocolSimple.dbo.tblDB101SimmonsResultSimple.PKDb101 > > > > > -- > John W. Colby > Colby Consulting > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > > -- Paul Hartland paul.hartland at googlemail.com From stuart at lexacorp.com.pg Thu Oct 29 16:59:05 2015 From: stuart at lexacorp.com.pg (Stuart McLachlan) Date: Fri, 30 Oct 2015 07:59:05 +1000 Subject: [dba-SQLServer] Invalid column name dbo., In-Reply-To: References: Message-ID: <563296A9.28553.413A4D8A@stuart.lexacorp.com.pg> You have a column #MovePKs.Zip You are trying to join on #MOvePKs.dbo.Zip -- Stuart On 29 Oct 2015 at 12:35, John Colby wrote: > > Msg 207, Level 16, State 1, Procedure usp_MovesSimmons, Line 72 > Invalid column name 'dbo'. > > I cannot see where there is a column named .dbo in the second select. > > > Create Table #MovePKs (PKMove int, St char(2), Zip char(5), MSA Char ... > insert into #MovePKs (PKMove, St, Zip, MSA, MSAPlusName) ... > from #MovePKs > Inner Join _DataSimmonsProtocolSimple.dbo.tblDB101SimmonsResultSimple > ON #MovePKs.dbo.PKMove = ^^^^^^ > _DataSimmonsProtocolSimple.dbo.tblDB101SimmonsResultSimple.PKDb101 From stuart at lexacorp.com.pg Thu Oct 29 17:09:12 2015 From: stuart at lexacorp.com.pg (Stuart McLachlan) Date: Fri, 30 Oct 2015 08:09:12 +1000 Subject: [dba-SQLServer] Invalid column name dbo. In-Reply-To: <563296A9.28553.413A4D8A@stuart.lexacorp.com.pg> References: , <563296A9.28553.413A4D8A@stuart.lexacorp.com.pg> Message-ID: <56329908.8843.414391CA@stuart.lexacorp.com.pg> Damn. Make that #MovePKs.PKMove and #MovePKs.dbo.PKMove On 30 Oct 2015 at 7:59, Stuart McLachlan wrote: > You have a column #MovePKs.Zip > > You are trying to join on #MOvePKs.dbo.Zip > > > -- > Stuart > > On 29 Oct 2015 at 12:35, John Colby wrote: > > > > > Msg 207, Level 16, State 1, Procedure usp_MovesSimmons, Line 72 > > Invalid column name 'dbo'. > > > > I cannot see where there is a column named .dbo in the second > > select. > > > > > > Create Table #MovePKs (PKMove int, St char(2), Zip char(5), MSA Char > ... > > insert into #MovePKs (PKMove, St, Zip, MSA, MSAPlusName) > ... > > from #MovePKs > > Inner Join > > _DataSimmonsProtocolSimple.dbo.tblDB101SimmonsResultSimple ON > > #MovePKs.dbo.PKMove = > ^^^^^^ > > > _DataSimmonsProtocolSimple.dbo.tblDB101SimmonsResultSimple.PKDb101 > > _______________________________________________ > dba-SQLServer mailing list > dba-SQLServer at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-sqlserver > http://www.databaseadvisors.com > >