Question

Locked

Sql Syntax

By EVGA ·
I am writing a Stored Procedure to execute a Script with If then Statements. I am having a Hard time with the Inital Select Statment at the end where I am trying to compare a variable in the Statement. There should be a way to write this I just don't know how?


SET @SearchSQL = 'Select FD__MED_SURR.Today_date, FD__MED_SURR.Med_Name, FD__MED_SURR.MED_QU, FD__MED_SURR.MedAction, FD__MED_SURR.Docaction , FD__MED_SURR.OP__DOCID, FD__MED_SURR.Clientkey, FD__MED_SURR.Admissionkey From FD__MED_SURR Where @Clientkey = Clientkey and @Admissionkey = Admissionkey'


at the end of the stored proc it executes the @SearchSQL

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Cast Your Variables.

by jgarcia102066 In reply to Sql Syntax

You have to cast your variables as some type of string variable such as varchar and then surround them with the appropriate delimiters.

If your @Admissionkey and @Clientkey are integers this should work. If the integer values are greater than 10 places then increase the 10 to accommodate.

SET @SearchSQL = 'Select FD__MED_SURR.Today_date, FD__MED_SURR.Med_Name, FD__MED_SURR.MED_QU, FD__MED_SURR.MedAction, FD__MED_SURR.Docaction , FD__MED_SURR.OP__DOCID, FD__MED_SURR.Clientkey, FD__MED_SURR.Admissionkey From FD__MED_SURR Where ' + CAST(@Clientkey AS VARCHAR(10)) + ' = Clientkey and ' + CAST(@Admissionkey AS VARCHAR(10)) + ' = Admissionkey'


If the variables are some type of string variable then use the following:

SET @SearchSQL = 'Select FD__MED_SURR.Today_date, FD__MED_SURR.Med_Name, FD__MED_SURR.MED_QU, FD__MED_SURR.MedAction, FD__MED_SURR.Docaction , FD__MED_SURR.OP__DOCID, FD__MED_SURR.Clientkey, FD__MED_SURR.Admissionkey From FD__MED_SURR Where ''' + @Clientkey + ''' = Clientkey and ''' + @Admissionkey + ''' = Admissionkey'

Back to Web Development Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums