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 |
|
divyaram
Posting Yak Master
180 Posts |
Posted - 2010-04-08 : 07:32:25
|
| Hi all, I have created a sp to get the details of table name who column have varchar and nvarcharas data type.now i want to fetch each table name and the corresponding column name andhave to print a update statemnt like thisupdate dbo.FIS_Organizationset --Full_Name = dbo.DecodeUnicodeData(Full_Name,'#',';'),Abb_Name = dbo.DecodeUnicodeData(Abb_Name,'#',';') how i can loop inside the temp table and fetch each data procedure i have done is like thisCREATE PROCEDURE FSP_FunctionUpdate ASBEGINdeclare @Column VARCHAR(100)declare @Table VARCHAR(100)declare @txt AS VARCHAR(200) -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; CREATE TABLE #TEST ( ID INT IDENTITY(1,1), TABLENAME NVARCHAR(200), COLUMNNAME NVARCHAR(200) ) INSERT INTO #TEST ( TABLENAME , COLUMNNAME ) SELECT DISTINCT col.TABLE_NAME,col.COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS col INNER JOIN sys.tables tab on tab.name=col.TABLE_NAME WHERE tab.type ='u' and col.DATA_TYPE in ('varchar','text','nvarchar','char') SELECT * FROM #TEST ENDGOplease help me in this issueRegards,Divya |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-08 : 07:42:43
|
Assuming all your tables are in the dbo schema, then:SELECT 'UPDATE dbo.' + TABLENAME + ' SET ' + COLUMNNAME + ' =dbo.DecodeUnicodeData(' + COLUMNNAME + ',''#'','';'')' FROM #TESTThere are 10 types of people in the world, those that understand binary, and those that don't. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-08 : 07:53:56
|
| do you need a separate temp table for this? I think seeing your query you can directly use INFORMATION_SCHEMA.COLUMNS view------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
divyaram
Posting Yak Master
180 Posts |
Posted - 2010-04-08 : 08:12:33
|
quote: Originally posted by visakh16 do you need a separate temp table for this? I think seeing your query you can directly use INFORMATION_SCHEMA.COLUMNS view------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
i have done like this....how we can use view for thatALTER PROCEDURE FSP_FunctionUpdate ASBEGINdeclare @Column VARCHAR(100)declare @Table VARCHAR(100)declare @txt AS VARCHAR(200)DECLARE @MIN INT, @MAX INT -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; CREATE TABLE #TEST ( ID INT IDENTITY(1,1), TABLENAME NVARCHAR(200), COLUMNNAME NVARCHAR(200) ) INSERT INTO #TEST ( TABLENAME , COLUMNNAME ) SELECT DISTINCT col.TABLE_NAME,col.COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS col INNER JOIN sys.tables tab on tab.name=col.TABLE_NAME WHERE tab.type ='u' and col.DATA_TYPE in ('varchar','text','nvarchar','char') SET @MIN =1 SELECT @MAX =COUNT(*) FROM #TEST WHILE @MIN <=@MAX BEGIN SELECT @Column = COLUMNNAME, @Table=TABLENAME FROM #TEST WHERE ID =@MIN SET @txt = 'UPDATE '+@Table +' SET '+@Column+'='+'dbo.DecodeUnicodeData'+'('+@Column+','+'''#'','';'''+')' PRINT @TXT SET @MIN=@MIN +1 END ENDGORegards,Divya |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-08 : 08:20:49
|
| nope i was just suggesting you can use views directly in query rather than putting result in temp table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|