Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Server returns error during SQLCMD that doesn't fail when executed in SQL Management Studio

Posted on 2010-11-29
3
Medium Priority
?
639 Views
Last Modified: 2012-05-10
I've got a sql command file which executes without error when I run it inside Management Studio. However, when I run it using sqlcmd from a command prompt I get the following errors:

Msg 1023, Level 15, State 1, Server SQL1/SQLEXPRESS, Line 13
Invalid parameter 1 specified for datepart.
Msg 1023, Level 15, State 1, Server SQL1/SQLEXPRESS, Line 22
Invalid parameter 1 specified for datepart.

I have no idea why it would work in studio but not from sqlcmd - I've run similar commands without errors. Any ideas?

I'm posting the code for reference....

INSERT INTO CaseHistory
	(Region, EmployeeID, AsOfDate, Closed, Pending, Active, LateReport, AsOfWeek, EmpWk, YearWk)

-- All cases by region and employee when there is no BudgetID
select 
-- Region set to 'TBD' during insert, to be updated after INSERT
	'TBD',

-- CM (either the employee ID or the budget ID if a substitute is filling in on the case)
	case when (proj.budgetid is not null and exists (select 'X' from acm where proj.budgetid = acm.empid)) then proj.budgetid else ACM.EmpID end as CaseMgr, 

-- Record time stamp of the previous week's Sunday for AsOfDate
	GetDate()-(datepart("weekday",GetDate())+6)
 as AsOfDate, /*assume job runs on Sunday for previous week*/

-- Accumulate case counts for closed, pending, active, late status
	sum (case when proj.projectother3 like 'clos%' then 1 else 0 end) Closed,
	sum (case when proj.projectother3 like 'pend%' then 1 else 0 end) Pending,
	sum (case when left(proj.projectother3,4) not in ('clos','pend') then 1 else 0 end) Active,
	sum (case when left(proj.projectother3,1) in ('0','1')
		then 
			case when left(proj.projectother3,5) < Left(convert(varchar,Getdate()-4,10),5)
				/*compare to 4 days earlier to determine late, gives a grace period to allow for reports that weren't processed until Mon/Tue for previous week */
				then 1 else 0 end
			else 0 
		end) LateReport,

-- Store week as a value (NOTE: this may not be needed for reporting since YearWk contains this data)
	datepart("week",GetDate()
 -(datepart("weekday",GetDate()))

) as AsOfWeek,
	/*Using the week number of the beginning of the previous week since the job runs on Sunday of following week */

-- Build Primary Key field using the employeeID and YearWk fields concatenated
	(case when proj.budgetid is null then ACM.EmpID else proj.budgetid end)+ right(str(datepart("year",GetDate()-(datepart("weekday",GetDate()))),4),2) + '-' 
		+ left(convert(char,datepart("week",getdate()
-(datepart("weekday",GetDate()))
),1),2) as EmpWk,

-- Build YearWk to create unique time records for each week of each year, not just the week)
	right(str(datepart("year",GetDate()-(datepart("weekday",GetDate()))),4),2) + '-' 
		+ left(convert(char,datepart("week",getdate()
-(datepart("weekday",GetDate()))
),1),2) as YearWk

FROM ACM
	left outer join proj on ACM.EmpID = proj.employeeid
WHERE proj.projectstatus = 'Active' and proj.budgetid is null
GROUP BY /*ACM.Region, */ACM.EmpID, proj.budgetid
ORDER BY /*ACM.Region, */ACM.EmpID

Open in new window

0
Comment
Question by:Shannon Mollenhauer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
Humpdy earned 500 total points
ID: 34233767
try get rid of your quotes in your datepart

datepart(weekday,GetDate())
not
datepart("weekday",GetDate())
0
 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 500 total points
ID: 34233784
Do ont enclose week, year, weekday, etc. in quotes...

e.g.
datepart(week,getdate()...
0
 

Author Closing Comment

by:Shannon Mollenhauer
ID: 34234457
I'm splitting the points since you both posted at nearly the same time. Gee - I can't believe I couldn't find any answers like this with Google. Guess that's why I pay for EE every month! Thanks.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question