Bir tablonun alan isimlerini virgül ile ayrılmış listelenmesi.

by nurgun 17. August 2009 12:30

Bir tablonun kolonlarını virgül ile ayrılmış olarak listelenmesini istiyorsanız alttaki sp'yi kullanabilirsiniz.

Sp'nin birinci parametresi tablo adı ikinci parametresi listeyi ne şekilde almak istediğinizi belirtebilirsiniz

Örnekler :

 

use AdventureWorks

exec us_col 'Person.Contact'

ContactID, NameStyle, Title, FirstName, MiddleName...

exec us_col 'Person.Contact',1

[ContactID], [NameStyle], [Title], [FirstName], [MiddleName]...

exec us_col 'Person.Contact',2

[ContactID] = rtrim(ltrim([ContactID])), [NameStyle] = rtrim(ltrim([NameStyle]))...

exec us_col 'Person.Contact',3 --Excel'den copy/past yapýnca gerekebiliyor

[ContactID] = ltrim(rtrim(replace ([ContactID],char(160),'')))...

exec us_col 'Person.Contact',4 --cursor ve insert'larda gerekebiliyor

@ContactID, @NameStyle, @Title, @FirstName, @MiddleName

exec us_col 'Person.Contact',5

@ContactID int, @NameStyle NameStyle, @Title nvarchar(8), @FirstName Name, @MiddleName Name

 

Sp'nin kodu:

 

create procedure us_col

       @TableName varchar(100),

       @t tinyint = null

as

/*

*/

if @t is null

       SELECT  STUFF((SELECT ', ' + name

       FROM sys.columns where object_id =object_id(@TableName)

             ORDER BY column_id

                    FOR XML PATH('')), 1, 1, '')

else if @t = 1

       SELECT  STUFF((SELECT ', [' + name + ']'

       FROM sys.columns where object_id =object_id(@TableName)

             ORDER BY column_id

                    FOR XML PATH('')), 1, 1, '')

else if @t = 2

       SELECT  STUFF((SELECT ', [' + name + '] = rtrim(ltrim([' + name + ']))'

       FROM sys.columns where object_id =object_id(@TableName)

             ORDER BY column_id

                    FOR XML PATH('')), 1, 1, '')

else if @t = 3

       SELECT  STUFF((SELECT ', [' + name + '] = ltrim(rtrim(replace ([' + name + '],char(160),'

             +char(39) +char(39)+ ')))'

       FROM sys.columns where object_id =object_id(@TableName)

             ORDER BY column_id

                    FOR XML PATH('')), 1, 1, '')

else if @t = 4

       SELECT  STUFF((SELECT ', ' +'@'+ name

       FROM sys.columns where object_id =object_id(@TableName)

             ORDER BY column_id

                    FOR XML PATH('')), 1, 1, '')

else if @t = 5

       SELECT  STUFF((SELECT ', ' +'@'+ c.name + ' ' + t.name +

             (case

                    when t.name in ('varchar', 'char')

                           then '('+ convert(varchar, c.max_length)+')'

                    when t.name in ('nvarchar', 'nchar')

                           then '('+ convert(varchar, c.max_length/2)+')'

                    else ''

             end)

       FROM sys.columns c

             left join sys.types t on c.user_type_id = t.user_type_id

             where c.object_id = object_id(@TableName)

             ORDER BY c.column_id

                    FOR XML PATH('')), 1, 1, '')

 

--THE END


 

 


 

Tags: , ,

MS SQL

Comments

4/23/2010 5:55:32 AM #

filing cabinets

Im impressed.  I dont think Ive met anyone who knows as much about this subject as you do.  Youre truly well informed and very intelligent.  You wrote something that people could understand and made the subject intriguing for everyone.  Really, great blog youve got here.

filing cabinets United States | Reply

4/25/2010 5:52:30 AM #

foam crib mattress

Howdy, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you prevent it, any plugin or anything you can advise? I get so much lately it's driving me mad so any assistance is very much appreciated.

foam crib mattress United States | Reply

4/27/2010 11:46:57 PM #

Rapidshare

I\'m happy I found this blog, I couldnt discover any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i\'m always look for people to check out my site. Please stop by and leave a comment sometime!

Rapidshare United States | Reply

4/28/2010 12:15:15 PM #

Solve a Problem

Your blog is outrageous!  I mean, Ive never been so entertained by anything in my life!  Your vids are perfect for this.  I mean, how did you manage to find something that matches your style of writing so well?  Im really happy I started reading this today.  Youve got a follower in me for sure!

Solve a Problem United States | Reply

5/2/2010 9:31:33 PM #

anniversary gifts

Dont take this the wrong way, but youre totally boring me here.  Dont get me wrong, I think what you have to say is valid...totally!  But, youve got to give me something to think about that involves images.  You know what they say, -A picture is worth a thousand words.-  You could cut down on the words if you just gave me a few pictures.

anniversary gifts United States | Reply

5/4/2010 5:39:17 PM #

divers watches

I must say, youve got one of the best blogs Ive seen in a long time.  What I wouldnt give to be able to create a blog thats as interesting as this.  I guess Ill just have to keep reading yours and hope that one day I can write on a subject with as much knowledge as youve got on this one!

divers watches United States | Reply

5/28/2010 6:59:46 PM #

download games

How-do-you-do, just needed you to know I have added your site to my Google bookmarks because of your extraordinary blog layout. But seriously, I think your site has one of the freshest theme I've came across. It really helps make reading your blog a lot easier.

download games United States | Reply

5/29/2010 9:18:37 PM #

Merchant Services

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

Merchant Services United States | Reply

5/30/2010 9:35:47 AM #

SEO Outsourcing

Im not gonna lie, Im really impressed.  Its rare for me to find something on the internet thats as entertaining and intriguing as what youve got here.  Your page is sweet, your graphics are great, and whats more, you use videos that are relevant to what youre saying.  Youre definitely one in a million, man!

SEO Outsourcing United States | Reply

5/31/2010 7:32:20 PM #

profit instruments

Ha ha. I'm impressed.

profit instruments United States | Reply

5/31/2010 8:53:51 PM #

cheap picture frames

I hope you never stop!  This is one of the best blogs Ive ever read.  Youve got some mad skill here, man.  I just hope that you dont lose your style because youre definitely one of the coolest bloggers out there.  Please keep it up because the internet needs someone like you spreading the word.

cheap picture frames United States | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Nurgün Özgür

Facebook

 

Nurgün Özgür Photo

1993'ten bu yana profesyonel olarak yazılım ve sistem yönetimi konularında çeşitli yerli ve yabanci kuruluşların farklı kademelerinde aktif olarak yer aldım. 2011 yılında emekliliğe ayrıldım.

 

Valid XHTML 1.0 Transitional


Tag cloud


Facebook ile giriş