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.
Author |
Topic |
meberg66219
Yak Posting Veteran
65 Posts |
Posted - 2010-12-09 : 13:31:02
|
Using Server OS 2003, and SQL Server 2005 Server Management Studio I created the following stored procedure:USE [campusvue]GO/****** Object: StoredProcedure [dbo].[AddedStudent] Script Date: 12/09/2010 10:18:51 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: MBergeron-- Create date: 12/08/2010-- Description: Number of Students Added Per Day to SyStudent Table-- =============================================ALTER PROCEDURE [dbo].[AddedStudent]AS -- Add the parameters for the stored procedure hereDECLARE @TodayDayOfWeek INTDECLARE @EndOfPrevWeek DateTimeDECLARE @StartOfPrevWeek DateTime --get number of a current day (1-Monday, 2-Tuesday... 7-Sunday)SET @TodayDayOfWeek = datepart(dw, GetDate())--get the last day of the previous week (last Sunday)SET @EndOfPrevWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())--get the first day of the previous week (the Monday before last)SET @StartOfPrevWeek = DATEADD(dd, -(@TodayDayOfWeek+6), GetDate())SELECT DATENAME(weekday, DateAdded) AS AddDate, DATEPART(wk, DateAdded) AS WeekNumber, DATEPART(year, DateAdded)AS YearNumber, COUNT(SyStudentID) AS NewStudentsFROM dbo.SyStudentWHEREdbo.SyStudent.DateAdded BETWEENCONVERT(VARCHAR, @StartOfPrevWeek,7)ANDCONVERT(VARCHAR, @EndOfPrevWeek+1,7)GROUP BY DATENAME(weekday, DateAdded), DATEPART(wk, DateAdded), DATEPART(year, DateAdded)ORDER BYYearNumber,WeekNumber,AddDateWhen I execute the procedure I get data similar to this:AddDate= FridayWeekNumber= 49YearNumber=2010NewStudents=47AddDate=MondayWeekNumber=49YearNumber=2010NewStudents=64This is the data that I need. However, I am trying to take this information and place it in an Excel SpreadSheet.I need to create a view on a different SQL server which links back to the view I would like to create from this data outpu.Can anyone help me with the syntax to get this stored procedure output either in a table or as a view? Any assistance is greatly appreciated.Thank you. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
meberg66219
Yak Posting Veteran
65 Posts |
Posted - 2010-12-09 : 15:06:04
|
USE [campusvue]GO/****** Object: StoredProcedure [dbo].[AddedStudent] Script Date: 12/09/2010 10:18:51 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: MBergeron-- Create date: 12/08/2010-- Description: Number of Students Added Per Day to SyStudent Table-- =============================================ALTER PROCEDURE [dbo].[AddedStudent]AS -- Add the parameters for the stored procedure hereDECLARE @TodayDayOfWeek INTDECLARE @EndOfPrevWeek DateTimeDECLARE @StartOfPrevWeek DateTime --get number of a current day (1-Monday, 2-Tuesday... 7-Sunday)SET @TodayDayOfWeek = datepart(dw, GetDate())--get the last day of the previous week (last Sunday)SET @EndOfPrevWeek = DATEADD(dd, -@TodayDayOfWeek, GetDate())--get the first day of the previous week (the Monday before last)SET @StartOfPrevWeek = DATEADD(dd, -(@TodayDayOfWeek+6), GetDate())SELECT DATENAME(weekday, DateAdded) AS AddDate, DATEPART(wk, DateAdded) AS WeekNumber, DATEPART(year, DateAdded)AS YearNumber, COUNT(SyStudentID) AS NewStudentsFROM dbo.SyStudentWHEREdbo.SyStudent.DateAdded BETWEENCONVERT(VARCHAR, @StartOfPrevWeek,7)ANDCONVERT(VARCHAR, @EndOfPrevWeek+1,7)GROUP BY DATENAME(weekday, DateAdded), DATEPART(wk, DateAdded), DATEPART(year, DateAdded)ORDER BYYearNumber,WeekNumber,AddDateCREATE Table dbo.StudentsAdded1 (DATENAME(weekday, DateAdded), DATEPART(wk, DateAdded), DATEPART(year, DateAdded))INSERT INTOdbo.StudentsAdded1 (DATENAME(weekday, DateAdded), DATEPART(wk, DateAdded), DATEPART(year, DateAdded))EXEC[dbo].[AddedStudent]and I get these errors:Msg 173, Level 15, State 1, Procedure AddedStudent, Line 36The definition for column 'DATENAME' must include a data type.Msg 102, Level 15, State 1, Procedure AddedStudent, Line 38Incorrect syntax near '('.Am I supposed to create a new query for the temp table or can I execute within the view? If so, do I have the script in the proper place within the procedure? This is my first stored procedure and I have been researching this for days. I just can't seem to get it to work. |
 |
|
meberg66219
Yak Posting Veteran
65 Posts |
Posted - 2010-12-09 : 15:07:27
|
Pardon me. Execute within the Procedure not "view". |
 |
|
meberg66219
Yak Posting Veteran
65 Posts |
Posted - 2010-12-13 : 11:29:16
|
Could someone please assist me with this syntax for creating a table from the stored procedure above:CREATE TABLENewStudents2(AddDate nvarchar, WeekNumber nvarchar, YearNumber nvarchar, NewStudents nvarchar)INSERT INTONewStudents2 EXEC dbo.AddedStudentDECLARE @length nvarcharSELECT @length = syscolumns.length from syscolumns, sysobjects wheresyscolumns.id = sysobjects.id and sysobjects.name = 'AddStudent' andsyscolumns.name = 'NewStudents'SELECT * FROM AddStudent where (LEN(NewStudents)+LEN(AddDate))>@length |
 |
|
|
|
|
|
|