SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 IMPORT DATA TO EXTENDED PROPERTY
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stevenandler
Starting Member

USA
42 Posts

Posted - 11/04/2013 :  16:34:33  Show Profile  Reply with Quote
I am looking for a way to import data from a CSV or Excel spread sheet and add the data directly into an Extended field instead of a regular field in the table. for example: let's say I have a comma delimited field with the following info:

NDC_M_FORMULARY,CUSTOM_EXTSIG,Custom EXT SIG
NDC_M_FORMULARY,DRUG_CODE,Alternate key, user defined
NDC_M_FORMULARY,CHARGE_CODE,From the Charge code table

The first column is the table name
Second Column is the Column name in the table
The third column contains the description that I would like to store in the Value in the Extended Property Name "MS_Description"

Can someone please provide the T-SQL statement to accomplish this?

BTW,I did find the following T-SQL which returns the Extended description for a specific Extended Property

Here it is:
SELECT
[Table Name] = i_s.TABLE_NAME,
[Column Name] = i_s.COLUMN_NAME,
[Description] = s.value
FROM
INFORMATION_SCHEMA.COLUMNS i_s
LEFT OUTER JOIN
sys.extended_properties s
ON
s.major_id = OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME)
AND s.minor_id = i_s.ORDINAL_POSITION
AND s.name = 'MS_Description'
WHERE
OBJECTPROPERTY(OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME), 'IsMsShipped')=0
AND i_s.TABLE_NAME = 'NDC_M_FORMULARY'
ORDER BY
i_s.TABLE_NAME, i_s.ORDINAL_POSITION


Thank you.





stevenandler
Starting Member

USA
42 Posts

Posted - 11/12/2013 :  19:18:03  Show Profile  Reply with Quote
I was able to get my hands on the following code which does what I need. I hope someone else can also use it.


BEGIN
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE with override
GO

DECLARE @XL AS TABLE(
pk INT NOT NULL IDENTITY,
SchemaName VARCHAR(255),
TableName VARCHAR(255),
ColumnName VARCHAR(255),
Descr VARCHAR(1024))
INSERT INTO @XL (SchemaName, TableName, ColumnName, Descr)
select 'OGEN', TableName, ColumnName, Descr FROM OPENROWSET (
'MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=\\srvorders\Share\Temp;',
'SELECT TableName, ColumnName, Descr from formulary.csv');
DECLARE @I INT, @CNT INT, @SN as varchar(255), @CN as varchar(255), @TN as varchar(255), @DESCR as varchar(255)
SET @CNT = @@IDENTITY
SET @I = 1
WHILE @I <= @CNT
BEGIN
SELECT
@SN = SchemaName,
@TN = TableName,
@CN = ColumnName,
@DESCR = Descr
FROM @XL
WHERE pk = @I
EXECUTE sp_updateextendedproperty N'MS_Description', @DESCR, N'SCHEMA', @SN, N'TABLE', @TN, N'COLUMN', @CN
SET @I = @I + 1
END
END
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000