Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

maximum length reached using varchar?

Status
Not open for further replies.

bunmiA

MIS
Apr 20, 2004
27
GB
I am running a dynamic sql query which I have declared as varchar(8000). The query is longer than 8000 characters long. Does anyone have any ideas of how I can code this.

Thanks
Bunmi
 
I'm thinking you'll have to declare a second string variable and break up the query, then execute them together at the end. Something like:

declare @SetString varchar(8000), @SetString2 varchar(8000)
select @SetString = N'Select top 2 * from table'
select @SetString2 = N' where fname=''Bob'''
print @SetString + @SetString2
exec('sp_executesql N''''' + @SetString + '' + @SetString2 + '')

You may need to do this before you execute the two strings:

set @SetString = replace(@SetString,'''','''''')
set @SetString = replace(@SetString2,'''','''''')

This is to ensure the quotes inside your qureies are accounted for properly as the exec statement will mess all the quotes up in your queries.

Fill in whatever query you like in the strings to test this out. Let me know if this works for you.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top