C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Databases »

Single Stored Procdeure for Insert in to database Tables


Posted Date: 19 Mar 2005    Resource Type: Articles    Category: Databases
Author: Gokul DasMember Level: Bronze    
Rating: 1 out of 5Points: 10




Point to ConsiderWhile using SP the column values should be in the same order as
in Table.The values should be a comma seperated single string
Replace with NULL if no value for a particular field




SP Source Code

CREATE PROC sp_insert_to_table(@tableName varchar(100),@columnValues nvarchar(4000)) as
set nocount on
DECLARE @string nvarchar(3000), --for storing the first half of INSERT statement
@stringData nvarchar(3000), --for storing the data(VALUES) related statement
@dataType nvarchar(1000), --data types returned
@colData nvarchar(3000),@colName nvarchar(3000),
@Query nvarchar(4000),@str_identityval varchar(100),
@str_identitycol varchar(100),@identity varchar(100),@INDEX INT,
@SLICE nvarchar(4000),@flag_check int

set @flag_check=1
--Declare a cursor to retrieve column specific information for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR SELECT column_name,data_type FROM information_schema.columns WHERE table_name = @tableName

create table #tmptablemax(idval int)

create table #temptable(tempData nvarchar(4000))--for inserting the values for comma seperating

SET @string='INSERT '+@tableName+'('
SET @stringData=''

OPEN cursCol

FETCH NEXT FROM cursCol INTO @colName,@dataType
if @flag_check=1
BEGIN
set @identity='(SELECT COLUMNPROPERTY(OBJECT_ID('''+@tableName+'''),'''+@colName+''','+'''IsIdentity'''+'))'
set @str_identitycol=@colName
END

IF @@fetch_status<>0
BEGIN
PRINT 'Table '+@tableName+' Not found,Processing skipped.'
CLOSE cursCol
DEALLOCATE cursCol
RETURN
END
IF rtrim(@columnValues)=''
BEGIN
PRINT 'Column Values Missing'
CLOSE cursCol
DEALLOCATE cursCol
RETURN
END
set @flag_check=@flag_check+1
WHILE @@FETCH_STATUS=0
BEGIN
set @string=@string + @colName+','
FETCH NEXT FROM cursCol INTO @colName,@dataType
END
CLOSE cursCol
DEALLOCATE cursCol
SET @query =substring(@string,0,len(@string)) + ')VALUES('INSERT into #tmptablemax exec(@identity)

SET @query=(SELECT REPLACE(@query,@str_identitycol+',',''))
--SET @str_identityval=(select * from #tmptablemax)
-- HAVE TO SET TO 1 SO IT DOESNT EQUAL Z -- FIRST TIME IN LOOP
SELECT @INDEX = 1
-- Following as null values cause issues
WHILE @INDEX !=0
BEGIN
IF @columnValues='NULL'
SET @columnValues=''
-- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER
SELECT @INDEX = CHARINDEX(',',@columnValues)
-- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE
IF @INDEX !=0
SELECT @SLICE = LEFT(@columnValues,@INDEX - 1)
ELSE

SELECT @SLICE =@columnValues
SET @query=@query+''''+@SLICE+''''+','
-- CHOP THE ITEM REMOVED OFF THE MAIN STRING
SELECT @columnValues = RIGHT(@columnValues,LEN(@columnValues) - @INDEX)
-- BREAK OUT IF WE ARE DONE
IF LEN(@columnValues) = 0 BREAK

END
SET @query = substring(@query,0,len(@query)) +')'

exec sp_executesql @query --load and run the built query*/
GO






Thanks to many topics that i had gone through in the Net and used some of them along with my idea





Responses

Author: Durai Mathavan.R    21 Apr 2005Member Level: Bronze   Points : 0
for example beginners will not know why you use 'set nocount on 'and what is flag and begin trans etc


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: An Indepth coverage on Triggers
Previous Resource: An Indepth coverage on User-Defined Functions
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use