By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,574 Members | 1,977 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,574 IT Pros & Developers. It's quick & easy.

function help

P: 8
Hi! everybody

I have two tables in my data base. One table that keeps track of indian communities and the other table that keeps track of its community members. Each indian community is identified by a 3 digit number in the community table, And each community member is identified by a 10 digit number in the membership table. Here is what I would like to to. I would like to have the database check that the first 3 digits starting from the left to right of each community members number to make sure it matches with an indian community number in the community table before posting it in the database.

Jan 5 '09 #1
Share this Question
Share on Google+
2 Replies

Expert 2.5K+
P: 2,878
What do you have so far?

-- CK
Jan 5 '09 #2

Expert 100+
P: 692

You can write a stored procedure for inserting a record into a database.
Call the following function in order to check the 10 digit number in the membership table.

Expand|Select|Wrap|Line Numbers
  1. Create Function CheckId(@MainId varchar)
  2. returns int
  3. as
  4. Begin
  5.         Declare @v_subId varchar(3);
  6.         Declare @v_Id varchar(3);
  8.         --Get SubId from main Id.Here you will get first 3 characters of the string.
  9.         Select @v_subId =Substring(@MainId,1,3)
  11.          --check that if above Id exists in the communitytable
  12.         Select @v_Id = Id from  communitytable Where Id is like @v_subId 
  13.         If  @v_newId Is Not NULL 
  14.         BEGIN
  15.             Return 1
  16.         END 
  17.         ELSE
  18.            BEGIN
  19.             Return 0
  20.         END 
  23. End
Jan 6 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.