SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


query help


query help

Author
Message
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 223
Hi All,

can any one help on below query

declare @tbl table (TBLID int identity(1,1),ID int,patientName varchar(10),age int,city varchar(100),Mobile int)

insert @tbl(ID,patientName,age,city,Mobile) select 1,'Ramesh',20,'HYD',12345678 union all
select 1,'Ramesh new',20,'HYDERABAD ' ,12345678 union all
select 1,'Ramesh new',20,'HYDERABAD ' ,87654321

select * from @tbl

TBLID ID patientName age city Mobile
1 1 Ramesh 20 HYD 12345678
2 1 Ramesh 24 HYD 12345678
3 1 Ramesh new 20 HYDERABAD 87654321


i want output as mentioned below format which columns data got changed

Columns OLDDATA NEWDATA
patientName Ramesh Ramesh new
City HYD HYDERABAD
Mobile 12345678 87654321


please prepare any dynamic query
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62073 Visits: 17954
Well you did a pretty decent job of posting ddl and sample data. However it is totally unclear what you want for output. What column in your table defines the order?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
hslawj
hslawj
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 102
Are you looking for something like OUTPUT? http://msdn.microsoft.com/en-us/library/ms177564.aspx

I know in cases when you run an UPDATE you can use this to view the deleted.fieldname (old version), inserted.fieldname (new version) etc.
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 223
declare @tbl table (TBLID int identity(1,1),ID int,patientName varchar(10),age int,city varchar(100),Mobile int)

insert @tbl(ID,patientName,age,city,Mobile)
select 1,'Ramesh',20,'HYD',12345678 union all
select 1,'Ramesh new',20,'HYDERABAD ' ,12345678 union all
select 1,'Ramesh new',20,'HYDERABAD ' ,87654321

select * from @tbl

TBLID, ID, patientName , age , city , Mobile
1 , 1 , Ramesh , 20 , HYD , 12345678
2 , 1 , Ramesh , 24 , HYD , 12345678
3 , 1 , Ramesh new, 20 , HYDERABAD , 87654321


i want output as mentioned below format which columns data got changed

input params @FromTBLID,@ToTBLID

if i will pass @FromTBLID=1,@ToTBLID=3 then it will display like below

Columns, OLDDATA , NEWDATA
patientName ,Ramesh ,Ramesh new
City ,HYD , HYDERABAD
Mobile ,12345678 ,87654321

if i will pass @FromTBLID=1,@ToTBLID=2 then it will display like below

Columns, OLDDATA , NEWDATA
age , 20 ,24
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 223
declare @tbl table (TBLID int identity(1,1),ID int,patientName varchar(10),age int,city varchar(100),Mobile int)

insert @tbl(ID,patientName,age,city,Mobile)
select 1,'Ramesh',20,'HYD',12345678 union all
select 1,'Ramesh new',20,'HYDERABAD ' ,12345678 union all
select 1,'Ramesh new',20,'HYDERABAD ' ,87654321

select * from @tbl

TBLID, ID, patientName , age , city , Mobile
1 , 1 , Ramesh , 20 , HYD , 12345678
2 , 1 , Ramesh , 24 , HYD , 12345678
3 , 1 , Ramesh new, 20 , HYDERABAD , 87654321


i want output as mentioned below format which columns data got changed

input params @FromTBLID,@ToTBLID

if i will pass @FromTBLID=1,@ToTBLID=3 then it will display like below

Columns, OLDDATA , NEWDATA
patientName ,Ramesh ,Ramesh new
City ,HYD , HYDERABAD
Mobile ,12345678 ,87654321

if i will pass @FromTBLID=1,@ToTBLID=2 then it will display like below

Columns, OLDDATA , NEWDATA
age , 20 ,24

please Create query for above requirement
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94049 Visits: 38955
How about you provide us with the code you have written so far in an attempt to solve your problem.

We are volunteers on this site, willing to help you in resolving you problem. We aren't here to do your work for you.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 223
here i am doing like dis


declare @tbl1 table (TBLID int identity(1,1),ID int,patientName varchar(10),age int,city varchar(100),Mobile int)

insert @tbl1(ID,patientName,age,city,Mobile) select 1,'Ramesh',20,'HYD',12345678 union all
select 1,'Ramesh new',20,'HYDERABAD ' ,12345678 union all
select 1,'Ramesh new',20,'HYDERABAD ' ,87654321

select * into #temp from @tbl1



select name into #Result from tempdb..syscolumns where id=OBJECT_ID('tempdb..#temp')

alter table #Result add oldvalue varchar(100), newvalue varchar(100)

declare @old varchar(max),@New varchar(max),@column varchar(100)='name'
declare @tbl table (Value varchar(max))

declare temp cursor local for
select name from #Result

open temp

fetch next from temp into @column


while @@FETCH_STATUS=0
begin

insert @tbl exec ('select '+@column+' from #temp ')
select top 1 @old= Value from @tbl order by Value desc
select top 1 @New= Value from @tbl order by Value asc
update #Result set oldvalue=@old,newvalue=@New where name=@column
delete from @tbl
fetch next from temp into @column
end
select * from #Result where oldvalue<>newvalue


drop table #temp ,#Result



can any help me

is there any best way
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62073 Visits: 17954
So you want to get the lowest of each value as the NewValue and the largest of each value as OldValue and then unpivot those results? Assuming that is what you want there is a decent way we can accomplish with some dynamic sql. It will at least let you get rid of that cursor.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62073 Visits: 17954
This produces the same output as your example.


if OBJECT_ID('MyResult') is not null
drop table MyResult

create table MyResult
(
TBLID int identity(1,1),
ID int,
patientName varchar(10),
age int,
city varchar(100),
Mobile int
)

insert MyResult(ID, patientName, age, city, Mobile)
select 1,'Ramesh',20,'HYD',12345678 union all
select 1,'Ramesh new',20,'HYDERABAD' ,12345678 union all
select 1,'Ramesh new',20,'HYDERABAD' ,87654321

declare @sql nvarchar(max)
select @sql = COALESCE(@sql + ' union all ', '') + 'select ''' + c.name + ''' as [Column], Max(Cast([' + c.name + '] AS VarChar(MAX))) as [OldValue], Min(Cast([' + c.name + '] AS VarChar(MAX))) as [NewValue] from MyResult having Min(Cast([' + c.name + '] AS VarChar(MAX))) <> Max(Cast([' + c.name + '] AS VarChar(MAX)))'
from sys.columns c
inner join sys.objects o on c.object_id = o.object_id and o.type = 'u'
where o.object_id = object_id('MyResult')
order by c.column_id

--select @sql

exec sp_executesql @sql




_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 223
thank u so much...........
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search