MsSQL

 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Tutorialized ForumsDatabasesMsSQL

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Tutorialized Forums Sponsor:
  #1  
Old July 17th, 2013, 07:34 AM
doneshsharma doneshsharma is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jul 2013
Posts: 1 doneshsharma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 47 sec
Reputation Power: 0
Create Dynamic Table

CREATE FUNCTION [dbo].[ufn_DemilitorToTable] ( @StringInput VARCHAR(8000), @Delimiter nvarchar(10))
RETURNS @OutputTable TABLE ( [String] VARCHAR(MAX) )
AS
BEGIN
DECLARE @String VARCHAR(MAX)
WHILE LEN(@StringInput) > 0
BEGIN
SET @String = LEFT(@StringInput,
ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput) - 1, -1),
LEN(@StringInput)))
SET @StringInput = SUBSTRING(@StringInput,
ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput), 0),
LEN(@StringInput)) + 1, LEN(@StringInput))

INSERT INTO @OutputTable ( [String] )
VALUES ( @String )
END

RETURN
END

--p_PrepareDynamicTable 'c1~nvarchar(100),c2,c3,c4',',','!'
CREATE PROC p_PrepareDynamicTable( @ColumnString nvarchar(MAX),
@ColumnNameSeperator nvarchar(10)=',',
@ColumnDataTypeSeperator nvarchar(10)='~')
AS
BEGIN
BEGIN TRY
IF ISNULL(@ColumnNameSeperator,'')=''
SET @ColumnNameSeperator=','

IF ISNULL(@ColumnDataTypeSeperator,'')=''
SET @ColumnDataTypeSeperator='~'


SELECT SUBSTRING(String, 1, CASE
WHEN CHARINDEX(@ColumnDataTypeSeperator, String) = 0 THEN LEN(String) + 1
ELSE CHARINDEX(@ColumnDataTypeSeperator, String)
END - 1)Column_Name,
CASE
WHEN CHARINDEX(@ColumnDataTypeSeperator, String) = 0 THEN NULL
ELSE SUBSTRING(String, CHARINDEX(@ColumnDataTypeSeperator, String) + 1, LEN(String))
END Data_Type
INTO #Table_Schema
FROM dbo.[ufn_DemilitorToTable] (@ColumnString, @ColumnNameSeperator)

CREATE TABLE #temp
(
dummy bit
);

DECLARE @Script AS nvarchar(max);
DECLARE @Script_prepare AS nvarchar(max);

SET @Script_prepare = 'Alter table #temp Add [?] [~];'
SET @Script = ''

SELECT @Script = @Script + REPLACE(REPLACE(@Script_prepare, '?', Column_Name), '[~]', CASE WHEN LTRIM(RTRIM(ISNULL(Data_Type, ''))) ='' THEN 'NVARCHAR(MAX)' ELSE Data_Type END)
FROM #Table_Schema

PRINT ( @Script )

EXEC (@Script)

PRINT 'Successful'

ALTER TABLE #temp
DROP COLUMN dummy;

SELECT *
FROM #temp;

DROP TABLE #temp

DROP TABLE #Table_Schema
END TRY

BEGIN CATCH
SELECT CAST(ERROR_NUMBER() AS NVARCHAR(500))
+ '-' + ERROR_MESSAGE()
END CATCH
END

Reply With Quote
Reply

Viewing: Tutorialized ForumsDatabasesMsSQL > Create Dynamic Table


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap