String checking

This is a discussion on String checking within the sqlserver-programming forums in Microsoft SQL Server category; I am creating a select statement based on a passed variable. I want to check the string to see if it contains a field from the table and if so add it to my select statement. Is there a function that I can use to check for my field I.e something like Declare @sql varchar(1000) Declare @mystring varchar( 500) Set @mystring = 'abc' Set @sql = Select ' if contains(@mystring, abc) Set @sql = @sql + 'abc'...

Go Back   Database Forum > Microsoft SQL Server > sqlserver-programming

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-28-2008, 05:07 AM
Default String checking

I am creating a select statement based on a passed variable.

I want to check the string to see if it contains a field from the table
and if so add it to my select statement. Is there a function that I can use
to check for my field

I.e something like

Declare @sql varchar(1000)
Declare @mystring varchar( 500)

Set @mystring = 'abc'
Set @sql = "Select '

if contains(@mystring, abc) Set @sql = @sql + 'abc'



Reply With Quote
  #2  
Old 08-28-2008, 05:16 AM
Default Re: String checking

Hi Paul,

Charindex , should do the job you want

Dave

Paul wrote:
> I am creating a select statement based on a passed variable.
>
> I want to check the string to see if it contains a field from the table
> and if so add it to my select statement. Is there a function that I can use
> to check for my field
>
> I.e something like
>
> Declare @sql varchar(1000)
> Declare @mystring varchar( 500)
>
> Set @mystring = 'abc'
> Set @sql = "Select '
>
> if contains(@mystring, abc) Set @sql = @sql + 'abc'
>
>
>

Reply With Quote
  #3  
Old 08-28-2008, 06:50 AM
Default Re: String checking

Thanks that does it.

Whilst I have been doing that , I have constructed a create #tbl string, and
executed it successfully, but the table does not exist when I try to select
from it.

I.e.
Set @sql = 'CREATE TABLE #tblMAIN (
[STAGE_1_START] [datetime] NULL ,
[ELEMENT] [varchar] (40) NULL
ON [PRIMARY]'

Exec(@sql)

Should this work, or is this not possible?

Paul


"dave ballantyne" wrote in message
news:uxrg%23ZOCJHA.1228-at-TK2MSFTNGP02.phx.gbl...
Hi Paul,

Charindex , should do the job you want

Dave

Paul wrote:
> I am creating a select statement based on a passed variable.
>
> I want to check the string to see if it contains a field from the table
> and if so add it to my select statement. Is there a function that I can
> use to check for my field
>
> I.e something like
>
> Declare @sql varchar(1000)
> Declare @mystring varchar( 500)
>
> Set @mystring = 'abc'
> Set @sql = "Select '
>
> if contains(@mystring, abc) Set @sql = @sql + 'abc'
>
>
>



Reply With Quote
  #4  
Old 08-28-2008, 06:55 AM
Default Re: String checking

Paul
declare @col as sysname

set @col='opendate'

If exists (
select * from sys.columns

where object_id=object_id('tblname')

and name =@col

)
................





"Paul" wrote in message
news:vfydnRrvksd0wSvVnZ2dnUVZ8sDinZ2d-at-bt.com...
>I am creating a select statement based on a passed variable.
>
> I want to check the string to see if it contains a field from the table
> and if so add it to my select statement. Is there a function that I can
> use to check for my field
>
> I.e something like
>
> Declare @sql varchar(1000)
> Declare @mystring varchar( 500)
>
> Set @mystring = 'abc'
> Set @sql = "Select '
>
> if contains(@mystring, abc) Set @sql = @sql + 'abc'
>
>
>



Reply With Quote
  #5  
Old 08-28-2008, 07:00 AM
Default Re: String checking

Hi,

That will work , and does work, but the #table is being drop when it
falls out of scope, the exec statement.

Dave

Paul wrote:
> Thanks that does it.
>
> Whilst I have been doing that , I have constructed a create #tbl string, and
> executed it successfully, but the table does not exist when I try to select
> from it.
>
> I.e.
> Set @sql = 'CREATE TABLE #tblMAIN (
> [STAGE_1_START] [datetime] NULL ,
> [ELEMENT] [varchar] (40) NULL
> ON [PRIMARY]'
>
> Exec(@sql)
>
> Should this work, or is this not possible?
>
> Paul
>
>
> "dave ballantyne" wrote in message
> news:uxrg%23ZOCJHA.1228-at-TK2MSFTNGP02.phx.gbl...
> Hi Paul,
>
> Charindex , should do the job you want
>
> Dave
>
> Paul wrote:
>> I am creating a select statement based on a passed variable.
>>
>> I want to check the string to see if it contains a field from the table
>> and if so add it to my select statement. Is there a function that I can
>> use to check for my field
>>
>> I.e something like
>>
>> Declare @sql varchar(1000)
>> Declare @mystring varchar( 500)
>>
>> Set @mystring = 'abc'
>> Set @sql = "Select '
>>
>> if contains(@mystring, abc) Set @sql = @sql + 'abc'
>>
>>
>>

>
>

Reply With Quote
  #6  
Old 08-28-2008, 07:16 AM
Default Re: String checking

Hi

I am using SQL2000 and if I add
select * from #tblMAIN

straight after I have executed @sql in the same window, it errors with
Invalid object name #tblmain

I.e.
The entire Sp

Declare @sql varchar(1000)

Set @sql = 'CREATE TABLE #tblMAIN (
[STAGE_1_START] [datetime] NULL ,
[ELEMENT] [varchar] (40) NULL)
ON [PRIMARY]'

Exec(@sql)

select * from #tblMAIN

Does it actually run within your environment?

Paul

"dave ballantyne" wrote in message
news:u5B$bUPCJHA.2480-at-TK2MSFTNGP02.phx.gbl...
Hi,

That will work , and does work, but the #table is being drop when it
falls out of scope, the exec statement.

Dave

Paul wrote:
> Thanks that does it.
>
> Whilst I have been doing that , I have constructed a create #tbl string,
> and executed it successfully, but the table does not exist when I try to
> select from it.
>
> I.e.
> Set @sql = 'CREATE TABLE #tblMAIN (
> [STAGE_1_START] [datetime] NULL ,
> [ELEMENT] [varchar] (40) NULL
> ON [PRIMARY]'
>
> Exec(@sql)
>
> Should this work, or is this not possible?
>
> Paul
>
>
> "dave ballantyne" wrote in message
> news:uxrg%23ZOCJHA.1228-at-TK2MSFTNGP02.phx.gbl...
> Hi Paul,
>
> Charindex , should do the job you want
>
> Dave
>
> Paul wrote:
>> I am creating a select statement based on a passed variable.
>>
>> I want to check the string to see if it contains a field from the table
>> and if so add it to my select statement. Is there a function that I can
>> use to check for my field
>>
>> I.e something like
>>
>> Declare @sql varchar(1000)
>> Declare @mystring varchar( 500)
>>
>> Set @mystring = 'abc'
>> Set @sql = "Select '
>>
>> if contains(@mystring, abc) Set @sql = @sql + 'abc'
>>
>>
>>

>
>



Reply With Quote
  #7  
Old 08-28-2008, 07:34 AM
Default Re: String checking

Hi

I have googled and found that the table only exists in the scope of the exec
statement, which is no use to me.
Is there a way of holding the table until the end of the sp?

Thanks

Paul

"Paul" wrote in message
news:r8ydnW61nreS5ivVnZ2dnUVZ8uydnZ2d-at-bt.com...
Hi

I am using SQL2000 and if I add
select * from #tblMAIN

straight after I have executed @sql in the same window, it errors with
Invalid object name #tblmain

I.e.
The entire Sp

Declare @sql varchar(1000)

Set @sql = 'CREATE TABLE #tblMAIN (
[STAGE_1_START] [datetime] NULL ,
[ELEMENT] [varchar] (40) NULL)
ON [PRIMARY]'

Exec(@sql)

select * from #tblMAIN

Does it actually run within your environment?

Paul

"dave ballantyne" wrote in message
news:u5B$bUPCJHA.2480-at-TK2MSFTNGP02.phx.gbl...
Hi,

That will work , and does work, but the #table is being drop when it
falls out of scope, the exec statement.

Dave

Paul wrote:
> Thanks that does it.
>
> Whilst I have been doing that , I have constructed a create #tbl string,
> and executed it successfully, but the table does not exist when I try to
> select from it.
>
> I.e.
> Set @sql = 'CREATE TABLE #tblMAIN (
> [STAGE_1_START] [datetime] NULL ,
> [ELEMENT] [varchar] (40) NULL
> ON [PRIMARY]'
>
> Exec(@sql)
>
> Should this work, or is this not possible?
>
> Paul
>
>
> "dave ballantyne" wrote in message
> news:uxrg%23ZOCJHA.1228-at-TK2MSFTNGP02.phx.gbl...
> Hi Paul,
>
> Charindex , should do the job you want
>
> Dave
>
> Paul wrote:
>> I am creating a select statement based on a passed variable.
>>
>> I want to check the string to see if it contains a field from the table
>> and if so add it to my select statement. Is there a function that I can
>> use to check for my field
>>
>> I.e something like
>>
>> Declare @sql varchar(1000)
>> Declare @mystring varchar( 500)
>>
>> Set @mystring = 'abc'
>> Set @sql = "Select '
>>
>> if contains(@mystring, abc) Set @sql = @sql + 'abc'
>>
>>
>>

>
>




Reply With Quote
  #8  
Old 08-28-2008, 07:45 AM
Default Re: String checking

Which is what i said...

I assume that you will not know the structure of the temp table at
design time which is why you are creating it in an exec statement.
Correct ?

If you dont know the structure, that would also imply that you are going
to attempt to do all the other data manipulation with dynamic sql ?

That being the the case , and my assumptions being true, that is very
bad practice. Take a step back , have a deep breath and re-evaluate
your process as a whole.

Dave


Paul wrote:
> Hi
>
> I have googled and found that the table only exists in the scope of the exec
> statement, which is no use to me.
> Is there a way of holding the table until the end of the sp?
>
> Thanks
>
> Paul
>
> "Paul" wrote in message
> news:r8ydnW61nreS5ivVnZ2dnUVZ8uydnZ2d-at-bt.com...
> Hi
>
> I am using SQL2000 and if I add
> select * from #tblMAIN
>
> straight after I have executed @sql in the same window, it errors with
> Invalid object name #tblmain
>
> I.e.
> The entire Sp
>
> Declare @sql varchar(1000)
>
> Set @sql = 'CREATE TABLE #tblMAIN (
> [STAGE_1_START] [datetime] NULL ,
> [ELEMENT] [varchar] (40) NULL)
> ON [PRIMARY]'
>
> Exec(@sql)
>
> select * from #tblMAIN
>
> Does it actually run within your environment?
>
> Paul
>
> "dave ballantyne" wrote in message
> news:u5B$bUPCJHA.2480-at-TK2MSFTNGP02.phx.gbl...
> Hi,
>
> That will work , and does work, but the #table is being drop when it
> falls out of scope, the exec statement.
>
> Dave
>
> Paul wrote:
>> Thanks that does it.
>>
>> Whilst I have been doing that , I have constructed a create #tbl string,
>> and executed it successfully, but the table does not exist when I try to
>> select from it.
>>
>> I.e.
>> Set @sql = 'CREATE TABLE #tblMAIN (
>> [STAGE_1_START] [datetime] NULL ,
>> [ELEMENT] [varchar] (40) NULL
>> ON [PRIMARY]'
>>
>> Exec(@sql)
>>
>> Should this work, or is this not possible?
>>
>> Paul
>>
>>
>> "dave ballantyne" wrote in message
>> news:uxrg%23ZOCJHA.1228-at-TK2MSFTNGP02.phx.gbl...
>> Hi Paul,
>>
>> Charindex , should do the job you want
>>
>> Dave
>>
>> Paul wrote:
>>> I am creating a select statement based on a passed variable.
>>>
>>> I want to check the string to see if it contains a field from the table
>>> and if so add it to my select statement. Is there a function that I can
>>> use to check for my field
>>>
>>> I.e something like
>>>
>>> Declare @sql varchar(1000)
>>> Declare @mystring varchar( 500)
>>>
>>> Set @mystring = 'abc'
>>> Set @sql = "Select '
>>>
>>> if contains(@mystring, abc) Set @sql = @sql + 'abc'
>>>
>>>
>>>

>>

>
>
>

Reply With Quote
  #9  
Old 08-28-2008, 11:21 AM
Default Re: String checking

Set @sql = 'CREATE TABLE #tblMAIN (
[STAGE_1_START] [datetime] NULL ,
[ELEMENT] [varchar] (40) NULL
ON [PRIMARY]

SELECT * FROM #tblMAIN '

Exec(@sql)


Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 07:48 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.