May 4, 2015 at 12:37 am
Hi,
I am trying to do the following:
DECLARE @MinBoundary DateTime, @s nvarchar(max)
declare @DBName sysname, @TableName sysname
select @DBName = 'MyDB', @TableName = 'MyTable'
select @s = '
SELECT Convert(DateTime, MIN(PRV.value))
FROM sys.partition_functions AS PF
JOIN sys.partition_parameters AS PP
ON PF.function_id = PP.function_id
JOIN sys.types AS T1
ON T1.system_type_id = PP.system_type_id
JOIN sys.types AS T2
ON T2.user_type_id= PP.user_type_id
JOIN sys.partition_range_values AS PRV
ON PP.function_id = PRV.function_id
AND PP.parameter_id = PRV.parameter_id
WHERE PF.name IN (''WBC_' + @TableName + '_pf'')'
print @s
EXECUTE sp_executesql @s, N'@MB DateTime OUTPUT', @MB=@MinBoundary OUTPUT
PRINT @MinBoundary
@Minboundary variable turns out to be null in the end.
It works if I print @s-2 query and run it this way, so the query is correct):
DECLARE @MinBoundary DateTime
SELECT @MinBoundary = Convert(DateTime, MIN(PRV.value))
FROM sys.partition_functions AS PF
JOIN sys.partition_parameters AS PP
ON PF.function_id = PP.function_id
JOIN sys.types AS T1
ON T1.system_type_id = PP.system_type_id
JOIN sys.types AS T2
ON T2.user_type_id= PP.user_type_id
JOIN sys.partition_range_values AS PRV
ON PP.function_id = PRV.function_id
AND PP.parameter_id = PRV.parameter_id
WHERE PF.name IN ('WBC_MyTable_pf')
select @MinBoundary
What could be wrong?
Thanks.
May 4, 2015 at 12:51 am
please refer to Eirikur response, i need more coffee i guess 🙂
May 4, 2015 at 1:27 am
Roust_m (5/4/2015)
Hi,I am trying to do the following:
DECLARE @MinBoundary DateTime, @s nvarchar(max)
declare @DBName sysname, @TableName sysname
select @DBName = 'MyDB', @TableName = 'MyTable'
select @s = '
SELECT Convert(DateTime, MIN(PRV.value))
FROM sys.partition_functions AS PF
JOIN sys.partition_parameters AS PP
ON PF.function_id = PP.function_id
JOIN sys.types AS T1
ON T1.system_type_id = PP.system_type_id
JOIN sys.types AS T2
ON T2.user_type_id= PP.user_type_id
JOIN sys.partition_range_values AS PRV
ON PP.function_id = PRV.function_id
AND PP.parameter_id = PRV.parameter_id
WHERE PF.name IN (''WBC_' + @TableName + '_pf'')'
print @s
EXECUTE sp_executesql @s, N'@MB DateTime OUTPUT', @MB=@MinBoundary OUTPUT
PRINT @MinBoundary
What could be wrong?
Thanks.
The error is that the variable @MB is not used in the dynamic query, it is missing!
😎
DECLARE @MinBoundary DateTime, @s nvarchar(max)
declare @DBName sysname, @TableName sysname
select @DBName = 'MyDB', @TableName = 'MyTable'
select @s = '
SELECT @MB = Convert(DateTime, MIN(PRV.value))
FROM sys.partition_functions AS PF
JOIN sys.partition_parameters AS PP
ON PF.function_id = PP.function_id
JOIN sys.types AS T1
ON T1.system_type_id = PP.system_type_id
JOIN sys.types AS T2
ON T2.user_type_id= PP.user_type_id
JOIN sys.partition_range_values AS PRV
ON PP.function_id = PRV.function_id
AND PP.parameter_id = PRV.parameter_id
WHERE PF.name IN (''WBC_' + @TableName + '_pf'')'
print @s
EXECUTE sp_executesql @s, N'@MB DateTime OUTPUT', @MB=@MinBoundary OUTPUT
select @MinBoundary
If there is no partition function for the query predicates then it will of course return NULL.
May 4, 2015 at 1:45 pm
Quick questions:
1. Why are you using Dynamic SQL when a normal query would be enough?
2. Why aren't you parametrizing your Dynamic SQL leaving it open to SQL Injection?
3. Why are you using IN for a single value?
Static SQL example:
SELECT @MinBoundary = Convert(DateTime, MIN(PRV.value))
FROM sys.partition_functions AS PF
JOIN sys.partition_parameters AS PP
ON PF.function_id = PP.function_id
JOIN sys.types AS T1
ON T1.system_type_id = PP.system_type_id
JOIN sys.types AS T2
ON T2.user_type_id= PP.user_type_id
JOIN sys.partition_range_values AS PRV
ON PP.function_id = PRV.function_id
AND PP.parameter_id = PRV.parameter_id
WHERE PF.name = 'WBC_' + @TableName + '_pf'
PRINT @MinBoundary
Dynamic SQL example parametrized query:
DECLARE @MinBoundary DateTime, @s nvarchar(max)
declare @DBName sysname, @TableName sysname
select @DBName = 'MyDB', @TableName = 'MyTable'
select @s = '
SELECT @MB=Convert(DateTime, MIN(PRV.value))
FROM sys.partition_functions AS PF
JOIN sys.partition_parameters AS PP
ON PF.function_id = PP.function_id
JOIN sys.types AS T1
ON T1.system_type_id = PP.system_type_id
JOIN sys.types AS T2
ON T2.user_type_id= PP.user_type_id
JOIN sys.partition_range_values AS PRV
ON PP.function_id = PRV.function_id
AND PP.parameter_id = PRV.parameter_id
WHERE PF.name IN (''WBC_'' + @TN + ''_pf'')'
print @s
EXECUTE sp_executesql @s, N'@MB DateTime OUTPUT, @TN sysname', @MB=@MinBoundary OUTPUT, @TN=@TableName
PRINT @MinBoundary
May 4, 2015 at 11:27 pm
Eirikur Eiriksson (5/4/2015)
The error is that the variable @MB is not used in the dynamic query, it is missing!
😎
Thanks!
May 4, 2015 at 11:35 pm
Luis Cazares (5/4/2015)
Quick questions:1. Why are you using Dynamic SQL when a normal query would be enough?
2. Why aren't you parametrizing your Dynamic SQL leaving it open to SQL Injection?
3. Why are you using IN for a single value?
Static SQL example:
SELECT @MinBoundary = Convert(DateTime, MIN(PRV.value))
FROM sys.partition_functions AS PF
JOIN sys.partition_parameters AS PP
ON PF.function_id = PP.function_id
JOIN sys.types AS T1
ON T1.system_type_id = PP.system_type_id
JOIN sys.types AS T2
ON T2.user_type_id= PP.user_type_id
JOIN sys.partition_range_values AS PRV
ON PP.function_id = PRV.function_id
AND PP.parameter_id = PRV.parameter_id
WHERE PF.name = 'WBC_' + @TableName + '_pf'
PRINT @MinBoundary
Dynamic SQL example parametrized query:
DECLARE @MinBoundary DateTime, @s nvarchar(max)
declare @DBName sysname, @TableName sysname
select @DBName = 'MyDB', @TableName = 'MyTable'
select @s = '
SELECT @MB=Convert(DateTime, MIN(PRV.value))
FROM sys.partition_functions AS PF
JOIN sys.partition_parameters AS PP
ON PF.function_id = PP.function_id
JOIN sys.types AS T1
ON T1.system_type_id = PP.system_type_id
JOIN sys.types AS T2
ON T2.user_type_id= PP.user_type_id
JOIN sys.partition_range_values AS PRV
ON PP.function_id = PRV.function_id
AND PP.parameter_id = PRV.parameter_id
WHERE PF.name IN (''WBC_'' + @TN + ''_pf'')'
print @s
EXECUTE sp_executesql @s, N'@MB DateTime OUTPUT, @TN sysname', @MB=@MinBoundary OUTPUT, @TN=@TableName
PRINT @MinBoundary
Thanks for your input. All valid questions.
1. I used dynamic sql because the table name is changing, but after closer look, you are right, static query should be enough.
2. Definitely need to improve on this.
3. The code was taken from another script, which I developed earlier. There may be multiple values if the logic changes.
May 5, 2015 at 1:50 pm
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply