Solved

MS SQL: make sure same or similar text values are not selected into a temp table when selecting from multiple tables

Posted on 2011-09-14
3
205 Views
Last Modified: 2012-05-12
Hi,

I have an application that basically works like this:

User One selects her favorite names from a set list of choices (we'll say two, but it changes).  I create a dynamic checkbox list that includes:

a) her correct answers that user two is trying to guess (tblRightAnswers)
b) additional answers from the set list of choices (tblSetChoices)
c) some silly and obviously wrong answers (tblFunnyStuff)

Getting the random table is easy (code attached), but the problem I'm having is that I can have her correct answer (let's say Jen) and also a "wrong" version from the tblSetChoices of Jen (since it was a set choice).  I can also have Jenny as silly answer.  I want to write the queries so that answers cannot be selected into the temp table if they are similar to answers already in the table.  I'm sure there's a way to do it through LEFT JOIN or something, but I'm no SQL specialist.  

Please help (code attached)

kmt
@questionID int = 0, 
	@userID nvarchar(20) = ''

	DECLARE @x int
	DECLARE @dropdown TABLE
	(
		[answerString] nvarchar(MAX),
		[realdeal] bit
	)

    -- Retrieve real values
    	INSERT INTO @dropdown(answerString, realdeal)
	SELECT      answerString, 1 
	FROM        tblRightAnswers
	WHERE     (questionID = @ questionID) AND (userID = @userID)
	
	-- Determine number of fake values
	SELECT @x = COUNT(*) FROM @dropdown
	SET @x = 6 - @x
	SET ROWCOUNT @x
	
	-- Retrieve fake values
	INSERT INTO @dropdown(answerString, realdeal)
	SELECT choice, 0 
	FROM tblSetChoices
WHERE questionID = @questionID	
ORDER BY NEWID()
	SET ROWCOUNT 2
	
	-- Retrieve fun fake values
	INSERT INTO @dropdown(answerString, realdeal)
	SELECT answerString, 0
	FROM      tblFunnyStuff
	WHERE     (questionID = @questionID)
	ORDER BY NEWID()
	SET ROWCOUNT 0
	
	SELECT * FROM @dropdown ORDER BY NEWID()

Open in new window

0
Comment
Question by:kmt333
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
Hi.

In your INSERT statements, you can add a WHERE condition to ensure that no answerString is = to or LIKE your current ones.

for example:
-- Retrieve fake values
INSERT INTO @dropdown(answerString, realdeal)
SELECT choice, 0
FROM tblSetChoices
WHERE questionID = @questionID      
AND NOT EXISTS (
   SELECT 1
   FROM @dropdown
   WHERE answerString = choice
)

ORDER BY NEWID()

OR ...

-- Retrieve fake values
INSERT INTO @dropdown(answerString, realdeal)
SELECT choice, 0
FROM tblSetChoices
WHERE questionID = @questionID      
AND NOT EXISTS (
   SELECT 1
   FROM @dropdown
   WHERE answerString LIKE ('%' + choice + '%')
   OR choice LIKE ('%' + answerString + '%')
)

ORDER BY NEWID()

You may be able to play with something like SOUNDEX() -- http://msdn.microsoft.com/en-us/library/ms187384.aspx -- but see if one of the above works for you.

Kevin
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
Comment Utility
Actually, for completeness, here is the SOUNDEX option.

-- Retrieve fake values
INSERT INTO @dropdown(answerString, realdeal)
SELECT choice, 0
FROM tblSetChoices
WHERE questionID = @questionID      
AND NOT EXISTS (
   SELECT 1
   FROM @dropdown
   WHERE SOUNDEX(answerString) = SOUNDEX(choice)
)

ORDER BY NEWID()

I tested: 'Jen' and 'Jenny' correlated correctly.
0
 
LVL 4

Author Closing Comment

by:kmt333
Comment Utility
Thank you both!  This did the trick
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
separate column 24 19
Date conversion in sql server 2012 6 25
Row-Level Security 2 15
Creating Alerts in sql sever 2 12
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now