4

I have a table like this :

keyword (table name) 
---------------- 
kid key 
---------------- 
1 asp.net 
2 php 
3 C#‎ 
4 vb

In a webform page, i have a textbox that user can insert string like below :

asp.net,php,C#‎,vb,css

i must insert this string in another table like below:

p-tag (table name)
----------------
pid kid
----------------
1 1
1 2
1 3
1 4


i must first check that those word in string exist in Keyword,or not. if yes insert them in p-tag table. I can perform this with C# code , but i want do with stored procedure.
Is it possible?
note : my english is weak, sorry.

asked Feb 2, 2014 at 11:57

1 Answer 1

5

I suggest passing the keywords as a table-value parameter. Refer to this article to find out more about it - Table-Valued Parameters. There's also a good C# example on how to use it. Then it is fairly easy to accomplish what you need with a single query. First create a type for table-value parameter:

 CREATE TYPE dbo.TableTypeName AS TABLE
 ( keyName nvarchar(50) )

Then use it in the procedure like that:

CREATE PROCEDURE sp_UpdateTags 
 (@tableParameter dbo.TableTypeName READONLY, @tagId INT)
AS
INSERT [p-tag](pid, kid)
SELECT @tagId, K.kid from @tableParameter tP
inner join [keyword] K on key = tP.keyName

This will insert only values which exist in keyword table.

Hope it helps!

answered Feb 2, 2014 at 12:18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.