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 |
|
barflyz
Starting Member
47 Posts |
Posted - 2010-05-19 : 14:20:00
|
| Hi all, I am connecting to a SQL server 2005 DB with a free tool I use to write the SQL. It is called WINSQL. Is there a way to schedule my SQL statement to run nightly? It has 4 different statements (drop table, re-create, set index etc) with a "GO" between each one. I can run manually easy but want to schedule every night at 2am.thanks! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-05-19 : 16:22:25
|
| I dont have that so I need to get it. Can I just paste my sql into the step command box? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-05-20 : 08:06:13
|
| I am running a tool called WINSQL |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-05-21 : 15:25:39
|
| I know have the agent but have no idea how to call out some simple SQL like on a schedule.drop table dbo.bds_diabetic_values_tmg; |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-05-21 : 15:30:18
|
| Here is the full query I want to run nightly. I want to keep this qry as is also.thanks--first drop the existing table--drop table dbo.bds_diabetic_values_tmggo--2nd, create the table dbo.bds_diabetic_values_tmg with one query----the diabetic view is not needed as everything was accompished with one qry below----this qry ends at line 321--select a.ID as PID, a.last as LASTNAME,a.first as FIRSTNAME, a.middle as MIDDLENAME,a.phone1 AS PHONE1,a.phone2 AS PHONE2,a.dob AS DATEOFBIRTH,a.sex AS SEX,(x.First + ' ' + x.Last) AS RESPONSIBLE_PROVIDER,z.obsvalue as A1C_VALUE1,z.obsdate as A1C_OBSDATE1,v.obsvalue as A1C_VALUE2,v.obsdate as A1C_OBSDATE2,u.obsvalue as LDL_VALUE1,u.obsdate as LDL_OBSDATE1,w.obsvalue as LDL_VALUE2,w.obsdate as LDL_OBSDATE2,s.obsvalue as DLDL_VALUE1,s.obsdate as DLDL_OBSDATE1,t.obsvalue as DLDL_VALUE2,t.obsdate as DLDL_OBSDATE2,p.obsvalue as POLDL_VALUE1,p.obsdate as POLDL_OBSDATE1,q.obsvalue as POLDL_VALUE2,q.obsdate as POLDL_OBSDATE2,m.obsvalue as MICRO_VALUE1_1,m.obsdate as MICRO_OBSDATE1,n.obsvalue as MICRO_VALUE2_1,n.obsdate as MICRO_OBSDATE2,k.obsvalue as EYEEXAM_DATA1,k.obsdate as EYEEXAM_OBSDATE1,l.obsvalue as EYEEXAM_DATA2,l.obsdate as EYEEXAM_OBSDATE2,i.obsvalue as FOOTEXAM_DATA1,i.obsdate as FOOTEXAM_OBSDATE1,j.obsvalue as FOOTEXAM_DATA2,j.obsdate as FOOTEXAM_OBSDATE2,e.InsuredId AS PINSID,e.Name as PINSCO,d.InsuredId AS SINSID,d.Name as SINSCO,r.InsuredId AS OINSID,r.Name as OINSCO,g.obsvalue as OPTHTHALMOLOGIST,f.diagnosis_codes AS DIAGNOSISCODESinto dbo.bds_diabetic_values_tmg from(select distinct (patientprofile.pid) as ID,patientprofile.birthdate AS dob,patientprofile.last,patientprofile.first,patientprofile.middle,patientprofile.phone1,patientprofile.phone2,patientprofile.phone3,patientprofile.sex,CASEWHEN DATEPART(day, patientprofile.birthdate) > DATEPART(day, getdate())THEN DATEDIFF(month, patientprofile.birthdate, getdate()) - 1 ELSE DATEDIFF(month, patientprofile.birthdate, getdate()) END / 12 as AGEfrom patientprofile INNER JOIN PROBLEM ON patientprofile.PID = PROBLEM.PIDwhere patientprofile.ISPATIENT = 'Y' AND patientprofile.PSTATUS='A' andproblem.stopreason is null andpatientprofile.LAST not in ('Test', 'Testing') andPROBLEM.CODE like '%250.%' or PROBLEM.CODE like '%357.2'or PROBLEM.CODE like '%362.0' or PROBLEM.CODE like '%366.41'or PROBLEM.CODE like '%648.0')aleft outer join--a1c results 1--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 28)ZWhere Z.ROWID =1group by pid,obsvalue,obsdate)zon a.id=z.pidleft outer join--a1c results 2--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 28)ZWhere Z.ROWID =2group by pid,obsvalue,obsdate)von a.id=v.pidleft outer join--ldl results 1--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 3891)ZWhere Z.ROWID =1group by pid,obsvalue,obsdate)uon a.id=u.pidleft outer join--ldl results 2--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 3891)ZWhere Z.ROWID =2group by pid,obsvalue,obsdate)won a.id=w.pidleft outer join--dldl results 1--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 21893)ZWhere Z.ROWID =1group by pid,obsvalue,obsdate)son a.id=s.pidleft outer join--dldl results 2--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 21893)zWhere Z.ROWID =2group by pid,obsvalue,obsdate)ton a.id=t.pidleft outer join--poldl results 1--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 30)ZWhere Z.ROWID =1group by pid,obsvalue,obsdate)pon a.id=p.pidleft outer join--poldl results 2--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 30)zWhere Z.ROWID =2group by pid,obsvalue,obsdate)qon a.id=q.pidleft outer join--micro results 1--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 13557)ZWhere Z.ROWID =1group by pid,obsvalue,obsdate)mon a.id=m.pidleft outer join--micro results 2--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 13557)zWhere Z.ROWID =2group by pid,obsvalue,obsdate)non a.id=n.pidleft outer join--eye results 1--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 2000009)ZWhere Z.ROWID =1group by pid,obsvalue,obsdate)kon a.id=k.pidleft outer join--eye results 2--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 2000009)zWhere Z.ROWID =2group by pid,obsvalue,obsdate)lon a.id=l.pidleft outer join--foot results 1--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 2541)ZWhere Z.ROWID =1group by pid,obsvalue,obsdate)ion a.id=i.pidleft outer join--foot results 2--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc, [OBSVALUE])as ROWID,* from obs where hdid = 2541)zWhere Z.ROWID =2group by pid,obsvalue,obsdate)jon a.id=j.pidleft outer join--optham results 1--(Select pid,obsvalue,obsdatefrom(Select Dense_Rank() Over(Partition by pid Order by [obsdate]desc)as ROWID,* from obs where hdid = 54169)ZWhere Z.ROWID =1group by pid,obsvalue,obsdate)gon a.id=g.pidleft outer join-- select for CONCATENATE PROBLEM CODES--(SELECT a.pid, dbo.Concat(pid) as diagnosis_codesFROM problem aGROUP BY a.pid)fon a.id=f.pidleft outer join(SELECT a.First, a.Last,b.pidFROM PatientProfile b LEFT JOIN DoctorFacility a ON b.DoctorId = a.DoctorfacilityID)xon a.id=x.pidleft outer join(SELECT c.Name, a.InsuredId, b.PIdFROM PatientInsurance a INNER JOIN PatientProfile bON a.PatientProfileId = b.PatientProfileIdINNER JOIN InsuranceCarriers c ON a.InsuranceCarriersId = c.InsuranceCarriersIdWHERE a.Inactive=0 AND a.OrderForClaims=1)eon a.id=e.pidleft outer join(SELECT c.Name, a.InsuredId, b.PIdFROM PatientInsurance a INNER JOIN PatientProfile bON a.PatientProfileId = b.PatientProfileIdINNER JOIN InsuranceCarriers c ON a.InsuranceCarriersId = c.InsuranceCarriersIdWHERE a.Inactive=0 AND a.OrderForClaims=2)don a.id=d.pidleft outer join(SELECT c.Name, a.InsuredId, b.PIdFROM PatientInsurance a INNER JOIN PatientProfile bON a.PatientProfileId = b.PatientProfileIdINNER JOIN InsuranceCarriers c ON a.InsuranceCarriersId = c.InsuranceCarriersIdWHERE a.Inactive=0 AND a.OrderForClaims=3)ron a.id=r.pidwhere a.AGE between 18 and 75group by a.ID, a.first,a.last,a.middle,a.phone1,a.phone2,a.dob,a.sex,z.obsdate,v.obsdate,u.obsdate,w.obsdate,s.obsdate,t.obsdate,p.obsdate,q.obsdate,m.obsdate,n.obsdate,k.obsdate,l.obsdate,i.obsdate,j.obsdate,f.diagnosis_codes,(x.First + ' ' + x.Last),e.Name, e.InsuredId,d.Name, d.InsuredId,r.Name, r.InsuredId,z.obsvalue,v.obsvalue,u.obsvalue,w.obsvalue,s.obsvalue,t.obsvalue,p.obsvalue,q.obsvalue,m.obsvalue,n.obsvalue,k.obsvalue,l.obsvalue,i.obsvalue,j.obsvalue,g.obsvalue--set constraints on table-goALTER TABLE dbo.bds_diabetic_values_tmgALTER column pid numeric(19,0) NOT NULL;--set pkey on table--goALTER TABLE dbo.bds_diabetic_values_tmg ADD PRIMARY KEY(pid); |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-05-21 : 15:41:12
|
| How can I just call out the .sql file and run the query at 2am every night?please help! |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-05-21 : 16:34:13
|
| There are two ways to handle this:1. Create a job, add a job step, specify "Operating System" for type, and call your sql file via sqlcmd.exe in the command field, add a job schedule for 2am daily.2. Create a job, add a job step, specify "Transact-SQL script for type, copy/paste the contents of the sql file into the command field, add a job schedule for 2am daily..Here's an example sqlcmd call:sqlcmd.exe -Sserver1\instance1 -E -iC:\Temp\SomeFile.sql -Ddatabase1Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
|
|
|
|
|