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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Variable problem......

Author  Topic 

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2007-02-22 : 10:38:28
I am trying to create a procedure with a variable that is used as part of another variable:



ALTER PROC SP_AUDIT_TABLE @Table_Name varchar(150)
as
declare @SqlString varchar(8000),
@Total numeric

set @SqlString = 'set ' + @Total + '= (select count(*) from ' + @Table_Name+ ')'

print @SqlString
exec (@Sqlstring)
print @Total


exec SP_AUDIT_TABLE 'Supplier_Table'


Error:

Msg 8114, Level 16, State 5, Procedure SP_AUDIT_TABLE, Line 6
Error converting data type varchar to numeric.

Any suggestions?

Thanks!

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-22 : 10:50:10
[code]declare @SqlString nvarchar(4000),
@Total int

set @SqlString = N'select @Total = count(*) from ' + @Table_Name

Exec sp_executesql @SqlString, N'@Total int output', @Total Output

Select @Total
[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-02-22 : 10:56:01
set @SqlString = 'set ' + @Total + '= (select count(*) from ' + @Table_Name+ ')'
...
exec (@Sqlstring)

note that @Total in the outer layer (your Sproc) will be out of scope to the inner layer (the code executing inside your EXEC statement).

sp_ExecuteSQL gets around that by being able to pass parameters in, and back out again - as my willing assistant Harsh has just demonstrated

Kristen
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-22 : 11:02:50
quote:
Originally posted by Kristen

set @SqlString = 'set ' + @Total + '= (select count(*) from ' + @Table_Name+ ')'
...
exec (@Sqlstring)

note that @Total in the outer layer (your Sproc) will be out of scope to the inner layer (the code executing inside your EXEC statement).

sp_ExecuteSQL gets around that by being able to pass parameters in, and back out again - as my willing & able assistant friend Harsh has just demonstrated

Kristen



Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-02-22 : 11:04:22
Friends get paid less than assistants!!
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-22 : 11:09:56
quote:
Originally posted by Kristen

Friends get paid less than assistants!!



Almost none...!
But if that was the purpose, I would have joined Rent-a-Coder instead.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2007-02-22 : 11:11:13
Hi I Try to run the following admendments but still an error:


ALTER PROC SP_AUDIT_TABLE @Table_Name varchar(150)
as
declare @SqlString varchar(8000),
@Total int

set @SqlString = N'select @Total = count(*) from ' + @Table_Name

Exec sp_executesql @SqlString, N'@Total int output', @Total Output

select @Total

exec SP_AUDIT_TABLE 'Supplier_Table'


Error:
Msg 214, Level 16, State 2, Procedure sp_executesql, Line 8
Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'.

Also, what does the N value represent?

Thanks
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-22 : 11:14:43
What is @statement? Where is it defined?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2007-02-22 : 11:17:04
I have no idea!

Try running the script and see what you return. I haven't declare or even produced @Statement....
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-22 : 11:20:34
In case you haven't noticed, in my first reply I changed,

@SqlString varchar(8000)

to

@SqlString nvarchar(4000)

Make this change and it should run fine.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2007-02-22 : 11:50:51
Thanks Harsh, thats great.

I have tried to incorporate this into my procedure and am getting errors like:
Msg 8115, Level 16, State 6, Procedure SP_AUDIT_TABLE, Line 46
Arithmetic overflow error converting nvarchar to data type numeric.



--exec SP_AUDIT_TABLE 'Supplier_Table'

ALTER PROC SP_AUDIT_TABLE
@Table_Name varchar(150)
as
declare @SqlString nvarchar(4000),
@Column_name varchar(150),
@Counter numeric,
@Total int,
@Max int

--Create Audit Table if not exists
If Not Exists (Select NAME From Sysobjects WHERE NAME = 'Table_Audit')
Begin
Create Table Table_Audit
(
Table_Name varchar(200),
Total_Count numeric,
Column_Name varchar(200),
Blank_Quantity int,
Percentage_Blank float,
Distinct_Count int,
Percentage_Distinct float,
Min_Length int,
Max_Length int,
Min_value nvarchar(400),
Max_Value nvarchar(400),
Nulls int,
Date_run datetime
)
end

--truncate table...
truncate table Table_Audit

set @SqlString = N'select @Total = count(*) from ' + @Table_Name
Exec sp_executesql @SqlString, N'@Total int output', @Total Output

set @SqlString = N'select @Max = count(*) from information_schema.columns where table_name = ''' + @Table_Name + ''''
Exec sp_executesql @SqlString, N'@Max int output', @Max Output

set @Counter = 1

while @Counter <= @Max
Begin
set @SqlString = N'select @Column_name = Column_Name from information_schema.columns where table_name = ''' + @Table_Name + ''' and Ordinal_position = ' +@Counter
Exec sp_executesql @SqlString, N'@Column_name varchar(150) output', @Column_name Output
set @Sqlstring = '
select '''+@Table_Name+'''as Table_Name,
count(*) as Total_Count,
''' +@Column_Name+ '''as Column_Name,
sum(case when cast([' +@Column_Name+ '] as varchar) is not null and cast([' +@Column_Name+ '] as varchar) <> '''' then 0 else 1 end) as Blank,
left(cast(sum(case when cast([' +@Column_Name+ '] as varchar) is not null and cast([' +@Column_Name+ '] as varchar) <> '''' then 0 else 1 end) as numeric)
/ ' + cast(@Total as varchar) +' * 100,5) as Percentage_Blank,
count(distinct [' +@Column_Name+ ']) as [Distinct],
left(cast(count(distinct [' +@Column_Name+ ']) as numeric)/' + cast(@Total as varchar) +' * 100,5) as Percentage_Distinct,
min(len(cast([' +@Column_Name+ '] as varchar))) as Min_length,
max(len(cast([' +@Column_Name+ '] as varchar))) as Max_Length,
cast(min([' +@Column_Name+ ']) as varchar) as Min_Value,
cast(max([' +@Column_Name+ ']) as varchar) as Max_Value,
sum(case when [' +@Column_Name+ '] is null then 1 else 0 end) as [Nulls],
getdate() as Date_Run
from ' + @Table_Name
--print @Sqlstring
insert into Table_Audit
exec sp_executesql @Sqlstring
set @Counter = @Counter + 1
End

select * from Table_Audit


Msg 8115, Level 16, State 6, Procedure SP_AUDIT_TABLE, Line 46
Arithmetic overflow error converting nvarchar to data type numeric.

If anybody can help me with this, that would be awesome!!!

Many thanks!!

David
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-22 : 12:40:30
Can't understand what you are trying to achieve?

But many of those queries doesn't require to be dynamic.

For example, this:

set @SqlString = N'select @Column_name =  Column_Name from information_schema.columns where table_name = ''' + @Table_Name + ''' and Ordinal_position = ' +@Counter


can be converted into normal query like:

select @Column_name =  Column_Name 
from information_schema.columns
where table_name = @Table_Name
and Ordinal_position = @Counter


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -