SSRS - Save multi-value parameter to a temp Table - sql

I have a multi-value parameter #ID in a report, with 100 values, and I need to collect the values selected in a temp table to further process them, then display the parameters into the report. How can I save the parameters selected into a temp table, one value in each row?
ID:
1
2
3
...
100
Thank you in advance for your help!

If your dataset query is not a stored proc AND they values originally came from a table then you can do this.
Assuming the IDs came from say, a products table, then you could just do
SELECT ProductID INTO #temp FROM myProducts WHERE ProductID in (#ID)
This method is generally basis for most of my dataset queries although I would not use a temp table I would simply query my data and apply the parameter in the WHERE clause as above.

Related

Querying a SQL table and only transferring updated rows to a different database

I have a database table which constantly gets updated. I am looking to query only the changes/additions that have been made on rows with a specific attribute in a column. e.g. get the rows which have been changed/added, the 'description' column of which is "xyz". My end goal is to copy these rows to another table in another database. Is this even possible? The reason for not just querying and overwriting the rows in the other database is to avoid inefficiency.
What I have tried so far?
I am able to select query on the table to get the rows but it gives me all the rows, not the ones that have been changed or recently added. If i add these rows to the table in the other database, the only option I have is to overwrite the rows.
Log table logs the changes in a table but I can't put additional filters in SQL which tells me which of these changes are associated with 'description' column as 'xyz'.
Write your update statements to make use of OUTPUT to capture the before and after values and log them to a table of your choice.
Here is a really simple example update example that uses output to store the RowID, before and after values for the ActivityType column:
DECLARE #MyTableVar table (
SummaryBefore nvarchar(max),
SummaryAfter nvarchar(max),
RowID int
);
update DBA.dbo.dtest set ActivityType = 3
OUTPUT deleted.ActivityType,
inserted.ActivityType,
inserted.RowID
INTO #MyTableVar
select * From #MyTableVar
You can do it two ways
Have new date fields/columns like update_time and/or create_time(Can be defaulted if needed). These fields will indicate the status of the record. You need to save your previous_run_time and then your select query will look for records with update_time/create_time greater than previous_run_time, and then you can move these records to the new DB.
Have CDC turned on the source table, which is available by default in SQL server and then move only those records that have been impacted.

SQL Query to count multiple values from one table into specific view

I like to request your help. I can get the results seperated but now i want to create a query which has it perfect for a external person. my explanation:
I have a statistics database with in this database a table when some records comes in and each records has several columns with values etc...
Now one of these columns is called "MT"
MT Column can have only one of the following values per records: A,B,C,D,E
The records also have a columne called TotalAmount which indicate a size of a value outside the database. This TotalAmount column is numeric without decimals and can have a value between 1 and 10.000.
And the last part is the records it self, the table has X amount of records.
So Basicly i need to create a query which seperates each MT value and calculates the amount of records per MT and the sum of TotalAmount.
This is on SQL Server 2005.
Many thanks for your assistance!
Very hard to guess without a full db schema. But I think you need.
SELECT MT, Count(*), SUM (TotalAmout)
FROM YourTable
GROUP BY MT

Inserting a query with a 'static' form field into a table

i'm trying to keep a record of which customers have received which newsletters. I have a query called
TempQuery
I want to take the field
TempQuery.CustID
That consists of several numerical id's and combine it with two static values specified on a form
Forms![frmAddCorrespondence]![txtNID]
Forms![frmAddCorrespondence]![txtDate]
such that I end up with something like
CustID NID Date
1 5 28/03/2011
3 5 28/03/2011
14 5 28/03/2011
56 5 28/03/2011
Again the fields NID and Date will be the same values for each individual insert specified on frmAddCorrespondence and the CustID's are pulled from TempQuery.
I'd like this to be a query from which the data can be inserted into a log table of all our past correspondences.
Any help is greatly appreciated
Thanks, Rob
you want a full outer join between your temp query and the date and ID entered at runtime?
If so, you can pass the two new column values (ID and date) as parameters to your query. Just create a new query based on your existing one and add the two control references as new columns.
Is that what you wanted?
In response to your comment, yes, you can create a second append query in Access that takes all the columns from your query and adds the two parameters from the controls on your form.

SQL Query for filtering columns returned

I want to return columns based on some meta data in an other table. i.e. i have my table which contains 10 columns, and another table which contains those columns denormalise with metadata to do with them.
i.e.
Table - Car:
columns - Make,Model,Colour
and another table called "Flags" which has a row for each of the above columns and each row has a column for "IsSearchable" and "ShowOnGrid" - that sort of thing.
The query i want is one which will return all columns from the cars table that are flagged in the "Flags" table as "ShowInGrid"
----EDIT
Apologise, I should have stated that this is on SQL Server 2008.
Also, I dont want to have to physically state the columns which i would like to return, i.e. If i add a column to the car table, then add it into the Flags table and declare it to be searchable, I don't want to have to physically state in the SQL Query that i want to return that column, i want it to automatically pull through.
You need to use dynamic SQL; this can easily be done with a stored procedure.
Something like this might work:
Select
D.CarID,
Case D.ShowMake When True Then D.Make Else NULL END AS Make
...
From
(Select
C.CarID, C.Make, C.Model, C.Colour, F.IsSearchable, F.ShowOnGrid, F.ShowMake
From
Cars C
Inner Join
Flags F
On C.CarID = F.CarID) D
I didn't write in all the case statements and don't know how many flags you're working, but you can give it a try. It would require to filter on null values in your application. If you actually want the columns omitted on the basis of the Flag column value the other answer and comment are both right on. Either Dynamic SQL or build your query outside in another language first.

SQL Server Procedure returns multiple tables - Insert results into tables

I have a procedure that returns multiple tables; eg:
PROCEDURE Something AS
BEGIN
SELECT 1,2,3
SELECT 4,5
SELECT 9,10,11
END
I would like to take each table from the result and insert it into a series of tables/temp tables - one for each record set.
Is this possible?
You could create the temporary tables within the stored proc and push the records into that. If you are using the same session , the table would be available after the stored proc is finished.
Or you could create the temp tables before hand and call the sp to populate them.
if you Union the results together they would come out as one result set.
your second query only has 2 columns but this would need to be resolved either way as you put it into a table.
Check out Multiple Active Resultsets (MARS). It may do what you are looking for.
http://www.sqlteam.com/article/multiple-active-result-sets-mars
http://blogs.msdn.com/sqlprogrammability/archive/2006/05/01/MARSIntroduction1.aspx