Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help Required

Author  Topic 

nirene
Yak Posting Veteran

98 Posts

Posted - 2009-02-13 : 23:47:47
Hai All,
Please provide me a solution to this problem.I'm trying something like this.

DECLARE @Tbl Varchar(20)
SET @Tbl = '_HR' + Ltrim(Rtrim(Str(Round((999999999 * Rand() + 1), 0))))

IF EXISTS (Select 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME=@Tbl)
Exec('Drop Table ' + @Tbl)

Declare @Str Varchar(200)
Set @Str='Create Table ' + @Tbl + '(Ecode Varchar(12))'
Exec(@Str)

---Other processes

Set @Str='Select Ecode from ' + @Tbl

Declare Attendance Cursor For Exec(@Str)


Nirene


sridhar.dbe
Starting Member

34 Posts

Posted - 2009-02-14 : 00:36:00
is this what you want?

declare @str varchar(10)
SET @STR='dbo.ty'
if OBJECT_ID (@str) is not null
exec ('DROP TABLE '+ @STR)
exec('create table '+@str+' (x int)')
insert into ty values(1);
exec ('select * from '+@str)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-14 : 00:44:21
why do you need to create tables with random number? can you explain that?
Go to Top of Page

nirene
Yak Posting Veteran

98 Posts

Posted - 2009-02-14 : 00:48:11
No I wanna exec the @Str in a Cursor

Declare Attendance Cursor For Exec(@Str)
Something like this.

Nirene
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-14 : 00:50:51
quote:
Originally posted by nirene

No I wanna exec the @Str in a Cursor

Declare Attendance Cursor For Exec(@Str)
Something like this.

Nirene


dynamic sql in a cursor? whats it that you're trying to achieve?
Go to Top of Page

nirene
Yak Posting Veteran

98 Posts

Posted - 2009-02-14 : 00:51:12
quote:
Originally posted by visakh16

why do you need to create tables with random number? can you explain that?



Just to create a temporary table.Nothing specific about it.Just create and after all processes delete it.

Nirene
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-14 : 00:56:52
then why looping inside a cursor?
Go to Top of Page

nirene
Yak Posting Veteran

98 Posts

Posted - 2009-02-14 : 03:47:49
quote:
Originally posted by visakh16

then why looping inside a cursor?



I fetch data thru another SP and using this Cursor I wanna do some more process, that is why I need to Dynamic SQL in cursor.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-14 : 04:03:51
you dont need cursor for fetching data from sp. do you mean you want to do some procesing for each row of sp resultset?
Go to Top of Page

nirene
Yak Posting Veteran

98 Posts

Posted - 2009-02-15 : 00:44:01
quote:
Originally posted by visakh16

you dont need cursor for fetching data from sp. do you mean you want to do some procesing for each row of sp resultset?



I do know that Visakh. Yes based on the result set I have to do some process.
Go to Top of Page
   

- Advertisement -