Quantcast
Channel: SQLServerCentral » SQL Server 2008 » SQL Server 2008 - General » Latest topics
Viewing all 16406 articles
Browse latest View live

Dynamic sp reuse from table

$
0
0
Hi SQL Experts ,i want to store dynamic expression query in table ., then i need to call one by one.use tempdb -- cc_model_range_2_1 table :create table cc_model_range_2_1 (id int identity(1,1),cc_key varchar(50),country_code varchar(2),Created_Date date default getdate())insert into cc_model_range_2_1 (cc_key ,country_code,Created_Date) values ('67472468f9719675670bf91d816ec023','es','2016-09-22')insert into cc_model_range_2_1 (cc_key ,country_code,Created_Date) values ('67472468f9719675670bf91d816ec023','es','2016-09-22') insert into cc_model_range_2_1 (cc_key ,country_code,Created_Date) values ('67472468f9719675670bf91d816ec023','in','2016-09-22')select * from cc_model_range_2_1-- table Temp1:CREATE TABLE [dbo].[temp1]( [Id] [int] IDENTITY(1,1) NOT NULL, [Model] [varchar](50) NULL, [validation_id] [int] NULL, [rows_affected] [bigint] NULL, [status] [varchar](50) NULL, [validated_date] [datetime] default getdate()) ON [PRIMARY]-- table temp2:CREATE TABLE [dbo].[temp2]( [id] [int] IDENTITY(1,1) NOT NULL, [Validation] [varchar](50) NOT NULL, [created_date] [datetime] default getdate(), [validation_query] [nvarchar](max) NULL) ON [PRIMARY]insert into temp2 (validation) values('Duplicate Model in 2_1'),('Invalid Country Code in 2_1')update temp2 set validation_query = 'Anandselect cc_key ,Anand+ cast(@min_id as varchar(5)) + Anand,count(cc_key)cnt,AnandAnandFAILAnandAnand from cc_model_range_2_1 group by cc_key , created_date having created_date between AnandAnandAnand+cast(@from_dt as varchar(10))+AnandAnandAnand and AnandAnandAnand+cast(@expt_dt as varchar(10))+AnandAnandAnand and cc_key = AnandAnandAnand+@cc_key+AnandAnandAnand and count(cc_key) > 1Anand' where id = 1update temp2 set validation_query = 'Anandselect cc_key,Anand+ @min_id +Anand,count(*)cnt,AnandAnandFAILAnandAnand from cc_model_range_2_1 where created_date between AnandAnandAnand+cast(@from_dt as varchar(10))+AnandAnandAnand and AnandAnandAnand+cast(@expt_dt as varchar(10))+AnandAnandAnand and cc_key = AnandAnandAnand+@cc_key+AnandAnandAnand and country_code <> AnandAnandesAnandAnand or isnull(country_code ,AnandAnandAnandAnand)= AnandAnandAnandAnandAnand'where id = 2update temp2 set validation_query = replace(validation_query,'Anand','''') select * from cc_model_range_2_1select * from temp1select * from temp2-- Dynamic SP : (code is seems to be ok if i run this print statement speratly it is working well)declare @min_id int = (select MIN(id) from temp2)declare @max_id int = (select MAX(id) from temp2)declare @cc_key varchar(50) = '67472468f9719675670bf91d816ec023'declare @from_dt date = '2016-09-22'declare @expt_dt date = '2016-09-22'declare @validation_query varchar(max)declare @SQL varchar(max)while (@min_id <= @max_id )beginset @validation_query = (select Validation_query from temp2 where ID = @min_id)set @sql = 'insert into temp1 (Model,validation_id,rows_affected,[status]) '+ @validation_queryprint @validation_query-- exec @validation_queryprint @sql-- exec (@sql)set @min_id = @min_id+1end-- dynamic sp (while executing there is an error was display)declare @min_id int = (select MIN(id) from temp2)declare @max_id int = (select MAX(id) from temp2)declare @cc_key varchar(50) = '67472468f9719675670bf91d816ec023'declare @from_dt date = '2016-09-22'declare @expt_dt date = '2016-09-22'declare @validation_query varchar(max)declare @SQL varchar(max)while (@min_id <= @max_id )beginset @validation_query = (select Validation_query from temp2 where ID = @min_id)set @sql = 'insert into temp1 (Model,validation_id,rows_affected,[status]) '+ @validation_query-- print @validation_query exec @validation_query-- print @sql exec (@sql)set @min_id = @min_id+1endselect * from temp1-- expected output is :insert into temp1 (Model,validation_id,rows_affected,[status]) 'select cc_key ,'+ cast(@min_id as varchar(5)) + ',count(cc_key)cnt,''FAIL'' from cc_model_range_2_1 group by cc_key , created_date having created_date between '''+cast(@from_dt as varchar(10))+''' and '''+cast(@expt_dt as varchar(10))+''' and cc_key = '''+@cc_key+''' and count(cc_key) > 1'-- hot code format (for understanding):insert into temp1 (Model,validation_id,rows_affected,[status]) select cc_key ,1,count(cc_key)cnt,'FAIL' from cc_model_range_2_1 group by cc_key , created_date having created_date between '2016-09-22' and '2016-09-22' and cc_key = '67472468f9719675670bf91d816ec023' and count(cc_key) > 1insert into temp1 (Model,validation_id,rows_affected,[status]) select cc_key,2,count(country_code)cnt,'FAIL' from cc_model_range_2_1 group by cc_key , country_code ,created_date having created_date between '2016-09-22' and '2016-09-22' and cc_key = '67472468f9719675670bf91d816ec023' and country_code <> 'es' or isnull(country_code ,'')= ''select * from temp1select * from temp2

Find next date after a given date

$
0
0
I'm trying to find the next appointment, if any, after a patient "no shows". Here is the code to create a table of no show appointments and another table of future appointments for those patients. How can I list the no show appointments along with the next future appointment? There may be more that one no show per patient.[code="sql"]create table #NoShow(MRN varchar(10),ApptDt datetime)insert into #NoShow(MRN, ApptDt) values('128772','2015-08-21 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('128848','2016-03-15 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('128848','2016-04-22 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('220598','2016-01-28 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('220598','2016-08-18 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('226109','2016-03-10 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('226109','2016-04-07 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('226109','2016-07-26 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('229759','2015-01-06 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('229759','2015-03-30 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('229759','2015-04-30 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('229759','2015-11-04 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('229786','2016-07-28 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('270108','2015-11-27 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('271379','2015-06-01 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('271379','2015-06-08 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('271379','2015-08-10 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('271379','2016-05-19 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('271379','2016-06-30 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('279601','2015-06-04 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('300723','2016-08-22 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('300807','2015-04-13 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('300807','2015-05-19 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('300807','2015-11-16 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('308127','2016-09-09 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('308488','2015-04-06 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('308696','2015-01-29 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('321916','2015-07-01 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('321916','2016-05-20 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('325441','2016-04-20 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('325441','2016-07-08 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('325464','2016-05-12 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('326249','2015-08-24 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('326926','2015-06-25 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('326926','2015-08-27 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('327118','2016-06-10 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('328701','2015-05-08 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('328701','2016-07-21 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('329453','2016-02-10 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('330229','2015-12-28 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('330421','2015-03-06 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('330421','2015-06-02 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('331253','2016-02-17 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('331974','2015-07-30 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('331974','2015-08-21 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('332994','2016-04-14 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('332994','2016-06-27 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('334579','2016-07-21 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('335418','2016-04-25 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('336320','2015-05-29 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('336323','2016-09-06 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('337635','2015-02-13 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('337635','2015-12-31 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('337721','2015-08-31 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('339406','2015-09-14 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('341847','2015-01-19 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('341847','2015-04-13 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('341847','2015-11-12 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344057','2015-04-17 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344057','2015-06-18 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344057','2015-10-15 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344057','2016-01-13 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344057','2016-04-21 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344057','2016-04-25 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344057','2016-05-23 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344057','2016-07-11 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344057','2016-08-05 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344977','2015-11-30 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('344977','2016-02-11 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345001','2015-03-11 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345001','2015-03-18 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345001','2015-07-14 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345001','2016-04-11 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345001','2016-05-12 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345026','2015-06-09 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345026','2015-07-14 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345026','2015-12-01 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345188','2015-01-12 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345188','2016-03-25 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('345188','2016-08-22 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('349976','2016-07-20 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('350298','2015-12-21 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('350298','2016-05-02 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('350315','2015-03-27 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('350315','2016-09-19 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('350346','2016-08-05 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('350460','2015-06-04 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('350460','2015-09-04 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('350460','2015-09-18 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('351124','2015-05-19 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('351212','2016-08-15 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('352145','2016-04-06 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('352759','2015-02-19 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('352759','2015-08-27 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('352759','2015-09-03 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('352759','2016-02-18 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('352858','2015-01-19 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('352858','2015-02-24 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('352858','2015-03-13 00:00:00.000')insert into #NoShow(MRN, ApptDt) values('352864','2016-02-23 00:00:00.000') create table #FutureAppts(MRN varchar(10),ApptDt datetime)insert into #FutureAppts(MRN, ApptDt) values('128772','2015-11-10 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('128772','2015-12-04 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('128772','2016-04-11 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('128848','2016-09-20 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('220598','2016-02-03 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('220598','2016-04-25 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('220598','2016-05-31 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('226109','2016-06-22 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('229759','2015-09-09 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('229759','2015-10-07 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('229759','2015-11-24 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('229759','2016-01-20 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('229759','2016-03-11 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('229759','2016-08-04 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('229786','2016-09-09 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('270108','2015-12-01 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('270108','2016-03-11 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('270108','2016-04-01 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('270108','2016-06-29 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('270108','2016-07-18 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('270108','2016-08-31 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('270108','2016-09-21 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('271379','2015-06-22 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('271379','2015-08-12 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('271379','2015-08-17 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('271379','2015-08-21 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('271379','2015-12-29 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('271379','2016-03-22 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('271379','2016-06-13 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('279601','2016-03-18 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('279601','2016-04-15 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('279601','2016-08-16 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('300807','2015-07-13 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('308488','2015-11-16 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('308696','2015-07-30 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('308696','2016-09-02 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('321916','2015-08-31 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('325441','2016-07-06 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('325441','2016-07-25 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('325441','2016-08-12 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('325441','2016-09-12 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('325464','2016-07-20 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('325464','2016-08-05 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('326926','2016-02-23 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('326926','2016-04-28 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('328701','2015-06-11 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('328701','2015-06-25 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('328701','2016-06-14 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('329453','2016-09-13 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('330229','2016-07-21 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('330421','2015-03-25 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('330421','2016-04-18 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('331253','2016-05-24 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('331974','2016-07-25 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('336320','2015-06-03 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('336320','2015-10-16 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337635','2015-02-18 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337635','2015-02-23 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337635','2015-08-11 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337635','2015-09-30 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337635','2016-01-14 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337635','2016-01-28 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337635','2016-02-10 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337635','2016-07-11 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337635','2016-08-10 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337721','2015-11-04 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337721','2015-11-17 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337721','2015-11-19 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('337721','2016-03-02 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('339406','2015-09-24 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('339406','2016-05-13 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('341847','2015-04-06 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('341847','2015-04-28 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('341847','2015-06-22 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('341847','2015-10-29 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('341847','2016-01-04 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('341847','2016-02-18 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('341847','2016-06-16 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344057','2015-10-27 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344057','2015-11-03 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344057','2015-12-14 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344057','2016-03-24 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344057','2016-05-05 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344057','2016-06-09 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344057','2016-08-19 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344057','2016-08-26 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344977','2016-04-08 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344977','2016-04-25 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('344977','2016-04-28 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345001','2015-04-01 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345001','2015-08-11 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345001','2015-10-06 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345001','2016-01-12 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345001','2016-02-11 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345001','2016-06-08 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345001','2016-08-29 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345026','2015-11-16 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345026','2015-11-27 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345026','2016-01-05 00:00:00.000')insert into #FutureAppts(MRN, ApptDt) values('345026','2016-04-14 00:00:00.000')[/code]

[dbo].[sysusers] column issue

$
0
0
HIIn SQL server 2000 the sysuser table had an suid column. in Sql server 2008 that table changed to a sys view and the column name changed to the uid. the program that I am using is old and its refering to that column. is there anyway to alter those system view to map another column?thanks

Urgent Help Need for DB Restoration

$
0
0
Hello Experts,I am facing issues with database restoration using different server backup.We have SQL 2005 instance. I took backup of the database (Nearly 1 TB) using Litespeed.The command I used to backup execute master.dbo.xp_backup_database @database = 'TestDB', @filename = '\\NetworkPath\TestDB_9_22_2016.bak', @init = 1, @compressionlevel = 5I tried to restore in SQL 2008 instance. The paths are different so I used with move optionexec master.dbo.xp_restore_database@database = 'TestDB',@filename = '\\NetworkPath\TestDB_9_22_2016.bak',@with = 'replace',@with = 'move "Logicalfilename" to "F:\Data\TestDB.mdf"',@with = 'move "LogicalFilenameofLog" to "F:\Logs\TestDB.ldf"'I am receiving the following errorMsg -2147417259, Level 16, State 1, Line 0Failed to create destination folderWhen I execute the command verify EXEC master.dbo.xp_restore_verifyonly@filename = '\\Path'The backup set on file 1 is valid.Directory lookup for the file "D:\DATABASES\TestDB_1.LDF" failed with the operating system error 2(The system cannot find the file specified.).The path specified by "D:\DATABASES\TestDB.mdf" is not in a valid directory.Attempting to restore this backup may encounter storage space problems. Subsequent messages will provide details.I need help to resolve this issue and restore the DB

Before this meeting, each school had its own

$
0
0
[url=http://genglobal.org/content/fox-georgia-vs-ole-miss-li.ve-s-tre.am-college-football-onlineweek4..24-sep-2016]Georgia vs Ole Miss Live Streaming[/url]

Following the addition of the former AFL teams

$
0
0
[url=https://www.youtube.com/watch?v=QMyjjvnobV8]Bills vs Cardinals Live Streaming[/url] [url=https://www.youtube.com/watch?v=6i1HZeeknlA]Raiders vs Titans Live Streaming[/url][url=https://www.youtube.com/watch?v=-K94Fw67yA8]Browns vs Dolphins Live Streaming[/url][url=https://www.youtube.com/watch?v=-iUBlJGLecE]Ravens vs Jaguars Live Streaming[/url][url=https://www.youtube.com/watch?v=92LoZ_zQYSY]Packers vs Lions Live Streaming[/url] [url=https://www.youtube.com/watch?v=CPMYFykU7v4]Broncos vs Bengals Live Streaming[/url][url=https://www.youtube.com/watch?v=X9ZCTe8V7SE]Panthers vs Vikings Live Streaming[/url][url=https://www.youtube.com/watch?v=ZWl7TvTdxMc]Redskins vs Giants Live Streaming[/url]

Create DAcPac Packages

$
0
0
Hi Experts,Please let me know how the .dacpac package is being created using SSDT with required Pre/Post deployment scripts?Kindly share if there are few good online material available to start practicing such good deployment features at projects.

Sql Server 2008 R2 Column Encryption

$
0
0
Greetings dear all,I've a column in a table with type NVARCHAR(50), with values that now "people" decided to turn it in an encrypted value. The only way that I know in order to achieve that is using a VARBINARY column where store the encrypted value, so is there a way to get it done without adding a new column but just chenging the existing one with the values? :unsure:Kindly,Kaxtanhu.

While use SqlBulkCopy trigger not triggered

$
0
0
I have created a trigger for a particular table.While i insert a record the trigger is working perfectly but while i use SqlBulkCopy to upload records from excel to database the trigger is not working what should i do. this is my triggerALTER TRIGGER [dbo].[CreateDummyforOthers] ON [dbo].[StudentPersonal]AFTER INSERTASDECLARE @StudentID as bigintSET @StudentID=( SELECT @@IDENTITY)INSERT INTO dbo.StudentAcademic (StudentID) Values(@StudentID)INSERT INTO dbo.StudentOldAcademic (StudentID) Values(@StudentID)Thanks.

How to get a resultant URL using tsql

$
0
0
I was able to use Adam Machanic's logic to [url=http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/validate-a-url-from-sql-server.aspx]Validate a URL from SQL Server[/url], now I am trying to retrieve a resulting or forwarded URL where applicable using sp_OAGetProperty responseText, but am having trouble figuring out how to do it or if it's even do-able using tsql. Any advice/direction is appreciated.Thanks,D

Is it possible to create a database alias or synonym?

$
0
0
Hi Experts,Wanted to know if there is there any way to have database names aliases in SQL Server?For example, there is a database with Name: "database A". Can we give the same database an alias: "database B"?

SQL Server Call/ SSIS to REST API Web Service (Workday) - (Not Weather API)

$
0
0
Hi All,I'm looking to call a web service (REST API/SOAP) doesn't matter through SSIS/Stored procedure. SSIS does have a web service call but it only accepts simple WSDL files and cannot read other methods. I tried this through stored procedure and get other errors. Still working to get my SOAP message right I guess.[code="xml"]http://www.vishalseth.com/post/2009/12/22/Call-a-webservice-from-TSQL-(Stored-Procedure)-using-MSXML.aspx[/code]ZaapSys does have a Powerpack utility and it works but it's paid. I can build the Integration from the workday side but then it's a 2 way step like dump a csv, run a listener and grab the file.Does anyone have a better approach ?Thanks,V

Cross apply ??

$
0
0
Hi [b]I use :[/b]CROSS APPLY Data.nodes('/Data/Delivery_x0020_Method/Service_x005F_x0020_Note_x005F_x0020_additions_Delivery_x0020_Method_Option') m(Node2) WHERE ( Node2.value('Checked[1]', 'varchar(max)') = 'true')[b]On this XML [/b]Data> <Delivery_x0020_Method> <Service_x005F_x0020_Note_x005F_x0020_additions_Delivery_x0020_Method_Option> <Name>Face to Face </Name> <Checked>false</Checked>[b]But not sure how to setup the cross apply for this data(added level)?[/b]<Data> <If_x0020_Other>symptom management,coping skills</If_x0020_Other> <Delivery_x0020_Method> <Service_x005F_x0020_Note_x005F_x0020_additions_Delivery_x0020_Method_Option> <Name>Face to Face </Name> <Checked>true</Checked> Thanks

T-SQL: Pulling Data Based on Only One Value

$
0
0
Hello:The results of my first code block below are shown in the attached Excel spreadsheet called "Clause Results". I don't want the last two rows, for 266267. I only want the first two rows, for 0100012160.The second block of code below shows the results that I want--one iteration of the APTODCNM field. A picture of this is shown in the attached file called "One APTODCNM.png".The third block of code shows three iterations of APTODCNM. I don't want this. This is shown in the final attached file called "Three APTODCNMs.png".What sort of syntax do I place in the first block of code to tell it to return data where there is only one APTODCNM? Thank you! Much appreciated!John[code="sql"]DECLARE @AGE DATETIMEDECLARE @RUN DATETIMESET @AGE = '2015-09-30 00:00:00.000'SET @RUN = '2016-07-31 00:00:00.000'select RM20101.CUSTNMBR as [CustomerID], RM00101.CUSTNAME as [CustomerName], RM20101.DOCNUMBR as [DocumentNumber],CASE WHEN RM20101.RMDTYPAL > 6 and RM20101.DOCDATE < @AGE and RM20201.DATE1 > @AGE and RM20201.APTODCDT > @AGE AND RM20201.APFRDCDT < @AGEAND RM20101.DOCNUMBR not in (select RM20201.APFRDCNM from RM20201INNER JOIN RM20101 ON RM20201.CUSTNMBR = RM20101.CUSTNMBR AND RM20201.APTODCNM = RM20101.DOCNUMBRAND RM20201.ORAPTOAM = RM20101.ORTRXAMTGROUP BY RM20201.APTODCNM, RM20201.APTODCDT, RM20201.APFRDCNMHAVING COUNT(RM20201.APFRDCNM) = 1)and RM20101.ORTRXAMT <> RM20101.CURTRXAM AND RM20201.APFRMAPLYAMT <> RM20101.ORTRXAMT AND RM20101.DUEDATE <> ''THEN RM20201.APFRMAPLYAMT * -1 ELSE 0 ENDas [OPEN A/R]from RM20101INNER JOIN RM00101 on RM20101.CUSTNMBR = RM00101.CUSTNMBRINNER JOIN RM20201 on RM20101.DOCNUMBR = RM20201.APFRDCNM AND RM20101.CUSTNMBR = RM20201.CUSTNMBRLEFT OUTER JOIN CN00500 ON RM20101.CUSTNMBR = CN00500.CUSTNMBRWHERE (RM20101.VOIDSTTS = 0) and (RM20101.CUSTNMBR IN (' 320651', '00065', '00351', '00370', '0035829', '0015200', '0100010149', '0100011881', '0100011303', '0100011766', '0100012897','195145', '0100550714', '238624', '0100013309', '0100010453', '266267', '179520', '0100012517', '0100011519', '0100012160')) GROUP BY RM20101.CUSTNMBR, RM00101.CUSTNAME, RM00101.PYMTRMID, CN00500.CRDTMGR, RM00101.COMMENT1, RM00101.COMMENT2, RM20201.APTODCDT, RM20201.APTODCNM, RM20101.DOCNUMBR, RM20101.DOCDATE,RM20201.APFRDCDT, RM20101.RMDTYPAL, RM20201.DATE1, RM20101.ORTRXAMT, RM20101.CURTRXAM, RM20101.DUEDATE, RM20201.APPTOAMT,RM20201.APFRMAPLYAMT, RM20201.ORAPTOAMHAVING CASE WHEN RM20101.RMDTYPAL > 6 and RM20101.DOCDATE < @AGE and RM20201.DATE1 > @AGE and RM20201.APTODCDT > @AGE AND RM20201.APFRDCDT < @AGEAND RM20101.DOCNUMBR not in (select RM20201.APFRDCNM from RM20201INNER JOIN RM20101 ON RM20201.CUSTNMBR = RM20101.CUSTNMBR AND RM20201.APTODCNM = RM20101.DOCNUMBRAND RM20201.ORAPTOAM = RM20101.ORTRXAMTGROUP BY RM20201.APTODCNM, RM20201.APTODCDT, RM20201.APFRDCNMHAVING COUNT(RM20201.APFRDCNM) = 1)and RM20101.ORTRXAMT <> RM20101.CURTRXAM AND RM20201.APFRMAPLYAMT <> RM20101.ORTRXAMT AND RM20101.DUEDATE <> ''THEN RM20201.APFRMAPLYAMT * -1 ELSE 0 END<> 0[/code][code="sql"]select * from RM20101 where CUSTNMBR = '0100012160' AND DOCNUMBR IN ('P0192700A')select * from RM20201 where CUSTNMBR = '0100012160' AND APTODCNM IN ('P0519167')[/code][code="sql"]select * from RM20101 where CUSTNMBR = '266267' AND DOCNUMBR = 'G1676571'select * from RM20201 where CUSTNMBR = '266267' AND APFRDCNM = 'G1676571A'[/code]

Estimating Row Level compression ; sp_estimate_data_compression_savings vs sp_spaceused

$
0
0
I'm trying to assess the benefits of row level compression of a small subset of larger tables in a db. If I run [code="sql"]exec sp_estimate_data_compression_savings 'dbo','mytable',NULL, NULL, 'ROW' ; go[/code] and total the results I get objectname CurrentSize Compressed Size Space Savedmytable 4842712 3013904 1828808If I try and verify the current size using sp_spaceused name rows reserved data index_size unusedmytable 12805934 4843184 KB 3378240 KB 1464472 KB 472 KBThe figures look different , based on comparing reserved to current size , is that the correct correlation /am I reading this wrong ? Basically Im trying to verify how much I could potentially save as the results so far dont look that impressive ! thanks simon

Dynamically creating a @table Table from a current table

$
0
0
I have a script that gives me a string of the columns and types.I wan to use this result to create a declared tableDECLARE @listStr VARCHAR(Max)SELECT @listStr = COALESCE(@liststr+', ','') +COLUMN_NAME + ' ' + Data_type + ' ' +CASE WHEN data_type = 'char' THEN '('+ cast(character_maximum_length AS VARCHAR) +')' ELSE '' END + ' ' +CASE WHEN is_nullable = 'NO' THEN 'NOT NULL' ELSE '' END FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TEST' SELECT @listStrDECLARE @table1 TABLE(@liststr)How can i get my @liststr in to the @Table1

Test Function for Performance Statistics

$
0
0
Hello, I have a function, where I am planning to check the performance statistics like start and end time.I want to test with 10,000 rows. Pass some random values and call them 10 times and use it in join table and call again?Is there a way to create cursor to do this?FUNCTION dbo.Blabla( @DE1 Varchar(100), @DE2 Varchar(100))RETURNS TABLE ASRETURN ( SELECT substring(CAST(COL1 AS VARCHAR(20)),1,CAST(CHARINDEX('-',COL1) AS VARCHAR(20)) -1) AS [code] , substring(COL1,CHARINDEX('-',COL1)+1,(Len(COL1))) AS [lCode] FROM [Emp] A Where DE1=@DE1 and DE2=@DE2 AND DT = (Select MAX(DT) From dbo.Emp B Where A.DE1=B.DE1 and A.DE2=B.DE2))

Restore databases involved in Replications

$
0
0
Hi,I need to restore a couple of databases in Non Prod which has replication involved.Question is: Is there any smooth option available to restore DB with replication being setup properly without any issue?Kindly suggest.

Assign Tasks to Appropriate Service

$
0
0
I'm trying to allot tasks to time frames and it's not as easy as it sounds.I have a system where an Order can have one or many services. These services may run consecutively at the same site, concurrently at the same site, consecutively at the same site with a break or concurrently at different sites. People may carry out tasks at the sites as required. I've been asked to create a fact table that links these tasks to the sites. This itself is straightforward enough because there is essentially a one-to-one relationship between the task and the site. It becomes more complicated because I have been asked to include service ID's on the fact table. There is a one-to-many relationship between the site and services and because of this I start to get duplicate rows. A task may appear to have been done under many services and this is not always the case.I can narrow down some of the services using the start and end dates and assigning the task to a service depending on when it was created. This doesn't work however for tasks that were created either before a service began, after it ended or between services. In these cases it's been agreed to assign the task to either the first service that starts after it was created or the last service that finished before it was created. For a task that was created between two services it should be assigned to the last service before it was created. Where there are concurrent services at the same place, the task is assigned to the lowest service ID.My current thinking about how to do this is to create a temporary table with the details of each service. There will be a row with a start date of 2011-12-01 00:00:00.00 and a end date of the earliest service start date for an order, this 'pseudo-service will have the Service ID of the earliest service. There will be another row with a service start date of the latest service end date and the end date will be now. The service ID for this row will be the ID of the latest service. Any breaks in services will have a similar row. That way, when I join to the temporary table, I'll be able to filter using a date range to only return one service per task.Yes, fixing the data is the best way to solve this; no, it's not an option :crazy: The other, and my favoured approach, is to tell the user they can't have the service ID because they don't actually need it.[code="sql"]CREATE TABLE #Services( ServiceID INT ,OrderID INT ,StartDate DATETIME ,EndDate DATETIME)INSERT INTO #Services VALUES(100001,200001,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000') --- One site, one service ,(100002,200002,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000') --- Two sites, one service at each site,(100003,200002,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000') --- Two sites, one service at each site ,(100004,200003,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000')-- One site, break in service,(100005,200003,'2016-09-17 00:00:00.000','2016-09-20 00:00:00.000')-- One site, break in service,(100006,200004,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000')-- One site, concurrent services,(100007,200004,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000')-- One site, concurrent services,(100008,200005,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000')-- One site, consecutive services,(100009,200005,'2016-09-15 00:00:00.000','2016-09-20 00:00:00.000')-- One site, consecutive servicesCREATE TABLE #Sites( SiteID INT ,Address VARCHAR(15))INSERT INTO #Sites VALUES(300001,'1 Fake Street'),(300002,'2 Fake Street'),(300003,'3 Fake Street'),(300004,'4 Fake Street'),(300005,'5 Fake Street'),(300006,'6 Fake Street')CREATE TABLE #SiteXService( SiteID INT ,ServiceID INT)INSERT INTO #SiteXService VALUES(300001, 100001),(300002,100002),(300003,100003),(300004,100004),(300004,100005),(300005,100006),(300005,100007),(300006,100008),(300006,100009)CREATE TABLE #Tasks( TaskID INT ,SiteID INT ,TaskType VARCHAR(50) ,Created DATETIME)INSERT INTO #Tasks VALUES(400001,300001 ,'Fit the equipment','2016-09-01 15:00:00.000') -- Two different tasks at the same place,(400002,300001,'Remove the equipment','2016-09-14 15:15:00.000') -- Two different tasks at the same place,(400003,300002,'Fit the equipment','2016-09-01 19:15:00.000') -- Two different tasks at different places,(400004,300003,'Fit the equipment','2016-09-01 19:15:00.000') -- Two different tasks at different places,(400005,300004,'Fit the equipment','2016-09-01 19:15:00.000') -- Task created within a service date range,(400006,300004,'Fit the equipment','2016-09-16 19:15:00.000') -- Task created between two services ,(400007,300004,'Remove the equipment','2016-09-18 19:15:00.000') -- Task created within a service date range,(400008,300005,'Check the equipment','2016-09-06 19:15:00.000') -- Task created to site with concurrent services,(400009,300006,'Fit the equipment','2016-08-16 19:15:00.000') -- Task created before a service starts,(400010,300006,'Remove the equipment','2016-09-14 19:15:00.000') -- Task created within a service date range,(400011,300006,'Recover missing equipment','2016-09-21 19:15:00.000') -- Task created after a service endsCREATE TABLE #Results( SiteID INT ,ServiceID INT ,StartDate DATETIME ,EndDate DATETIME)/* What I'd like to create. */INSERT INTO #Results VALUES (300001,100001,'2011-12-01 00:00:00.000','2016-09-01 00:00:00.000') --- Start date of 2011-12-01 and end date of first service start,(300001,100001,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000') --Actual service row,(300001,100001,'2016-09-15 00:00:00.000',GETDATE()) --- Start date of last service end date and indefinite end,(300002,100002,'2011-12-01 00:00:00.000','2016-09-01 00:00:00.000'),(300002,100002,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000'),(300002,100002,'2016-09-15 00:00:00.000',GETDATE()),(300003,100003,'2011-12-01 00:00:00.000','2016-09-01 00:00:00.000'),(300003,100003,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000'),(300003,100003,'2016-09-15 00:00:00.000',GETDATE()),(300004,100004,'2016-12-01 00:00:00.000','2016-09-01 00:00:00.000')--- Start date of 2011-12-01 and end date of first service start,(300004,100004,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000') --Actual service row,(300004,100005,'2016-09-15 00:00:00.000','2016-09-17 00:00:00.000') -- Start date of end of previous service and end date of start of next,(300004,100005,'2016-09-17 00:00:00.000','2016-09-20 00:00:00.000')--Actual service row,(300004,100005,'2016-09-20 00:00:00.000',GETDATE()),(300005,100006,'2012-12-01 00:00:00.000','2016-09-01 00:00:00.000'),(300005,100006,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000'),(300005,100006,'2016-09-15 00:00:00.000',GETDATE()),(300005,100007,'2012-12-01 00:00:00.000','2016-09-01 00:00:00.000'),(300005,100007,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000'),(300005,100007,'2016-09-15 00:00:00.000',GETDATE()),(300006,100008,'2011-12-01 00:00:00.000','2016-09-01 00:00:00.000'),(300006,100008,'2016-09-01 00:00:00.000','2016-09-15 00:00:00.000'),(300006,100009,'2016-09-15 00:00:00.000','2016-09-20 00:00:00.000'),(300006,100009,'2016-09-20 00:00:00.000',GETDATE())SELECT r.ServiceID,t.TaskID,t.TaskType,dates.StartDate,t.Created ,dates.EndDate,dates.ServiceIDFROM #Results rJOIN #SiteXService sxs ON sxs.ServiceID = r.ServiceIDJOIN #Sites si ON si.SiteID = sxs.SiteIDJOIN #Tasks t ON t.SiteID = si.SiteIDCROSS APPLY( SELECT * FROM #Services s WHERE s.ServiceID = r.ServiceID) datesWHERE t.Created >= r.StartDateAND t.Created <= r.EndDateDROP TABLE #Services ,#Sites ,#SiteXService ,#Tasks ,#Results[/code]

Applying multiple date filters in sql query

$
0
0
Hello all,You expert opinion is much appreciated.I am running a report that covers the last two fiscal years. I want all the data for the last two years. The exclusion criteria is below.Exclude the Consultants who had not had any sales ONLY in the last year. Something like the below.WHERE ActivityDate<01-Sep-2016 AND (ConsultantName IS NOT NULL and ActivityDate>’01-04-2016’)The first line means I want all the activity before September this year (including previous years)BUTexclude the consultants who had null activity only for 2016.I know the above filter is not correct. What is the right way of doing it please?Many thanks in advance.
Viewing all 16406 articles
Browse latest View live