-
SProc - Ad hoc sql statement with COUNT for exec(@SQL) ??
Hello,
I need to get the count into a local variable:
Select @SQL = 'Select ' + @TotalRowCount + ' = Count(*) )' + ' From ' + @TableName + ' Where ' + @WhereClause
Exec(@SQL)
It complains about ‘…. Integer…’, but even if I use a varchar parm and convert Count to varchar in the sql statement, it still does not work. It does not like the = , or so it says.
Any help greatly appreciated,
Judith
-
SProc - Ad hoc sql statement with COUNT for exec(@SQL) ?? (reply)
If I understand correctly, you're trying to get a value into an @variable from a dynamic sql statement. This is not possible. The only variables you can use in a dynamic sql are those declared within the string itself...you cannot declare a variable outside, and then try to assign a value through a dynamic query.
------------
Judith Farber Abraham at 5/14/01 3:04:50 PM
Hello,
I need to get the count into a local variable:
Select @SQL = 'Select ' + @TotalRowCount + ' = Count(*) )' + ' From ' + @TableName + ' Where ' + @WhereClause
Exec(@SQL)
It complains about ‘…. Integer…’, but even if I use a varchar parm and convert Count to varchar in the sql statement, it still does not work. It does not like the = , or so it says.
Any help greatly appreciated,
Judith
-
SProc - Ad hoc sql statement with COUNT for exec(@SQL) ?? (reply)
------------
Ananth at 5/14/01 3:35:38 PM
If I understand correctly, you're trying to get a value into an @variable from a dynamic sql statement. This is not possible. The only variables you can use in a dynamic sql are those declared within the string itself...you cannot declare a variable outside, and then try to assign a value through a dynamic query.
------------
Judith Farber Abraham at 5/14/01 3:04:50 PM
Hello,
I need to get the count into a local variable:
Select @SQL = 'Select ' + @TotalRowCount + ' = Count(*) )' + ' From ' + @TableName + ' Where ' + @WhereClause
Exec(@SQL)
It complains about ‘…. Integer…’, but even if I use a varchar parm and convert Count to varchar in the sql statement, it still does not work. It does not like the = , or so it says.
Any help greatly appreciated,
Judith
-
SProc - Ad hoc sql statement with COUNT for exec(@SQL) ?? (reply)
If you are trying to cater for a multiude of WHERE clauses but minimising the coding you may want to look at the CASE statement.
------------
Ananth at 5/14/01 3:35:38 PM
If I understand correctly, you're trying to get a value into an @variable from a dynamic sql statement. This is not possible. The only variables you can use in a dynamic sql are those declared within the string itself...you cannot declare a variable outside, and then try to assign a value through a dynamic query.
------------
Judith Farber Abraham at 5/14/01 3:04:50 PM
Hello,
I need to get the count into a local variable:
Select @SQL = 'Select ' + @TotalRowCount + ' = Count(*) )' + ' From ' + @TableName + ' Where ' + @WhereClause
Exec(@SQL)
It complains about ‘…. Integer…’, but even if I use a varchar parm and convert Count to varchar in the sql statement, it still does not work. It does not like the = , or so it says.
Any help greatly appreciated,
Judith
-
SProc - Ad hoc sql statement with COUNT for exec(@SQL) ?? (reply)
Hey Judith -
This should work for you. FYI - I used the pubs db for this data...
DECLARE @Sql varchar(500), @Tbl varchar(20), @Where varchar(50)
SET @Tbl = 'Titles'
SET @Where = 'Type = ''Business'''
SET @sql = 'Declare @test int'+CHAR(13)+
'SELECT @Test = COUNT(*) FROM '+@tbl+' WHERE '+@Where+CHAR(13)+
'SELECT @Test'
EXEC(@sql)
------------
Judith Farber Abraham at 5/14/01 3:04:50 PM
Hello,
I need to get the count into a local variable:
Select @SQL = 'Select ' + @TotalRowCount + ' = Count(*) )' + ' From ' + @TableName + ' Where ' + @WhereClause
Exec(@SQL)
It complains about ‘…. Integer…’, but even if I use a varchar parm and convert Count to varchar in the sql statement, it still does not work. It does not like the = , or so it says.
Any help greatly appreciated,
Judith
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|