Dynamic Forms allows you to use the value of SOME QueryString parameters in your Default Value SQL Query. Specifically, you can use DSParam1, DSParam2 and DSParam3 with little concern, and you can use your own parameters IF and ONLY IF you FIRST insert the value of that parameter into a HIDDEN field (no other field type will work) on the form (using the Advanced Field Option "Retrieve values from querystring variable for this question". Then just use the parameter's tag in the query like this:
SELECT FirstName + ' ' + LastName AS DefaultValue FROM Users WHERE UserID = '$(DSParam1)'
Two important things to note: First, the query must select a single value and you must name it DefaultValue. Second, you should put apostrophes around the parameter value even though in the table it is numeric. I did this because if you don't have DSParam1 in the QueryString an invalid SQL statement will be generated. MSSQL will convert the parameter and the field type to compatible types, so this will execute successfully. If DSParam1 is not specifed, and NULL value will be returned and the field will have no default value.
The next logical question is "How do I specify a default value for DSParam1, so when it is not specified I can still get something as the default value for the field? The answer isn't quite as simple as the above query, but it CAN be done, and in three different ways (that I could think of). While we're at it, we will also try to handle the situation where an invalid UserID is specified and return the same default value.
1. Use a stored procedure. Create a stored procedure such as this:
CREATE PROCEDURE [dbo].[GetFullName] @UserID nvarchar(10) = 1 AS BEGIN IF @ItemID = '' SET @ItemID = 0 SELECT FirstName + ' ' + LastName AS DefaultValue FROM Users WHERE UserID = @UserID END
And enter this for the default value query:
exec GetFullName '$(DSParam1)'
If DSParam1 is not specified, or has no value, the Name for the user with UserID 1 will be returned. However, if a non-existent UserID is specified, the stored procedure will still return null, and there will be no default value. I couldn't easily come up with a solution for that problem, so I went to the next option.
2. Create a function such as this:
CREATE FUNCTION [GetFullName] ( @UserID nvarchar(10) = 1 ) RETURNS nvarchar(200) AS BEGIN DECLARE @Result nvarchar(200) IF @UserID = '' SET @UserID = 1 SELECT @Result = FirstName + ' ' + LastName FROM Users WHERE UserID = @UserID IF @Result IS NULL SELECT @Result = FirstName + ' ' + LastName FROM Users WHERE UserID = 1 RETURN @Result END
And the query would look like:
select dbo.GetFullName('$(DSParam1)') as DefaultValue
Again, if DSParam1 is not specified, the name for the user with UserID 1 is returned. But now if an invalid UserID is specified the first query will return null, the second query will be executed, and the same default user's name is returned. Just what was required, but there's a simpler option:
3. Do it all in the query itself.
If a query returns no data the return value is null. This can be used with the 'isnull' function like this:
select isnull((select FirstName + ' ' + LastName from Users where UserID = '$(DSParam1)'),(select FirstName + ' ' + LastName from Users where UserID = 1)) as DefaultValue
All in one neat package! If DSParam1 is not specified, or if an invalid value is specified, the first query will return null. The isnull function will then use the value of the second query, which returns your default user's name.
Which of the three options you use depends on your needs and your personal preferences, and on whether you have the ability to create stored procedures or functions. I prefer the third method for it's simplicity and because it keeps everything all in one place.
It took me hours to figure all this out. I hope this posting saves people the time I spent.
|