Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

query help Expand / Collapse
Author
Message
Posted Friday, March 8, 2013 11:11 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, April 20, 2014 10:08 PM
Points: 30, Visits: 158
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
Post #1428706
Posted Friday, March 8, 2013 12:42 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 13,272, Visits: 12,103
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 Moden's 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)
Post #1428738
Posted Friday, March 8, 2013 12:56 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 12, 2013 7:21 AM
Points: 2, 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.
Post #1428742
Posted Friday, March 8, 2013 11:08 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, April 20, 2014 10:08 PM
Points: 30, Visits: 158
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

Post #1428854
Posted Friday, March 8, 2013 11:09 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, April 20, 2014 10:08 PM
Points: 30, Visits: 158
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
Post #1428855
Posted Saturday, March 9, 2013 6:11 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:32 PM
Points: 23,227, Visits: 31,919
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.



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)
Post #1428877
Posted Monday, March 18, 2013 11:48 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, April 20, 2014 10:08 PM
Points: 30, Visits: 158
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
Post #1432287
Posted Monday, March 18, 2013 1:05 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 13,272, Visits: 12,103
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 Moden's 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)
Post #1432316
Posted Monday, March 18, 2013 1:54 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 13,272, Visits: 12,103
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 Moden's 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)
Post #1432327
Posted Wednesday, March 20, 2013 11:40 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, April 20, 2014 10:08 PM
Points: 30, Visits: 158
thank u so much...........
Post #1433373
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse