I have a custom data source which pulls out data form a flat file. The flat file contains a timestamp , source and data. I can use sp_execute to execute a select query against the flat file.
I'm currently using 2 stored procedures .
- one which runs a select * from flat_file into a temp table
- the other which does a select min/max from flat_file grouping by source into another temp file
Im using the data retrieved using the stored procedures in a SSRS report
Is is possible in a a single stored procedure to retrieve all the rows from the file within a date range and also identify the min/max values for each group retrieved ?e
yes, combine all logic into one procedure and return a join of your two temp tables. you don't give any code, column names, etc, so this is a guess:
CREATE PROCEDURE AllInOne
(
#param1....
)
--populate temp table 1
...
--populate temp table 2
...
SELECT
t1.*, t2.*
FROM #Temp1 t1
INNER JOIN #temp2 t2 ON t1.PK=t2.PK
ORDER BY ....
go
SSRS can only handle one resultset from a dataset.
Without using a temp table (like KM's answer), this is 2 calls to the database.
However, if i read you correct, the 2 resultsets are fundamentally different: the min/max is an operation on the 1st result set after filtering and not on the original data.
So, you could do this in SSRS if you are grouping in a table control by using Min and setting the Scope parameter as your grouping
Related
I know how to find the CREATE statement for a table in SQL Server but is there any place that stores the actual SQL code if I use SELECT INTO ... to create a table and if so how do I access it?
I see two ways of creating tables with SELECT INTO.
First: You know the Schema, then you can declare a #Table Variable and perform the Select INSERT
Second: You can create a temp table:
SELECT * INTO #TempTable FROM Customer
There are some limitations on the second choice:
- You need to drop the temp table afterwards.
- If there is a VARCHAR Column and the maximum number of characters of that given SELECT is 123 characters (example), and then you try to insert into the TEMP table afterwards with a greater number of characters, it will throw an error.
My recommendation is always declare a table in order to use, it makes it clear what is the intentions and increases readability.
My goal is to separate two types of data in table that is being sent to a stored procedure. In this table, I will have two kinds of records of type 1 and type 2, let's say.
I want to delete all data of type 2 from the inputted table but still have it stored in a separate temp table.
I know how to delete data with the following statement:
DELETE t
FROM #tags t
WHERE t.Type = 2
Is there a way to retrieve the deleted records so I can insert them into a separate temp table?
Otherwise I will have to have a separate code block before that looks like the following:
INSERT #dynamicTags(String)
SELECT String
FROM #tags t
WHERE t.Type = 2
Any ideas to combine the two above statements into one?
If using SQL Server you can do this with the OUTPUT clause:
DELETE t
FROM #tags t
OUTPUT DELETED.* INTO #MyTableVar
WHERE t.Type = 2
If you are using postgres you can use the returning clause:
http://www.postgresql.org/docs/9.3/static/sql-delete.html
I am trying to validate the data present in SQL Server table using a stored procedure.
In one of the validation rules, i have to check whether the value of a particular column is present in another table.
Suppose i have a staging table with following columns Cat_ID, Amount, SRC_CDE
I have a 'maintable' with following columns CatID , Cat_Name
I have to validate whether the Cat_ID present in staging table exists in the 'maintable' for each row
I am using the following statement to validate
if((Select count(*) from maintable where CatID= #Cat_id) >0 )
-- Do something if data present
I want to know if there is any better way of doing the above thing other than using a select query for every row.
Can i use some sort of an array where i can fetch all the CatID from maintable and the check instead of using a select query.
Thanks
Using a left join to list all the invalid rows.
select
staging.*
from
staging
left join maintable
on staging.catid=maintable.catid
where maintable.catid is null
So I have a table which has a bunch of information and a bunch of records. But there will be one field in particular I care about, in this case #BegAttField# where only a subset of records have it populated. Many of them have the same value as one another as well.
What I need to do is get a count (minus 1) of all duplicates, then populate the first record in the bunch with that count value in a new field. I have another field I call BegProd that will match #BegAttField# for each "first" record.
I'm just stuck as to how to make this happen. I may have been on the right path, but who knows. The SELECT statement gets me two fields and as many records as their are unique #BegAttField#'s. But once I have them, I haven't been able to work with them.
Here's my whole set of code, trying to use a temporary table and SELECT INTO to try and populate it. (Note: the fields with # around the names are variables for this 3rd party app)
CREATE TABLE #temp (AttCount int, BegProd varchar(255))
SELECT COUNT(d.[#BegAttField#])-1 AS AttCount, d.[#BegAttField#] AS BegProd
INTO [#temp] FROM [Document] d
WHERE d.[#BegAttField#] IS NOT NULL GROUP BY [#BegAttField#]
UPDATE [Document] d SET d.[#NumAttach#] =
SELECT t.[AttCount] FROM [#temp] t INNER JOIN [Document] d1
WHERE t.[BegProd] = d1.[#BegAttField#]
DROP TABLE #temp
Unfortunately I'm running this script through a 3rd party database application that uses SQL as its back-end. So the errors I get are simply: "There is already an object named '#temp' in the database. Incorrect syntax near the keyword 'WHERE'. "
Comment out the CREATE TABLE statement. The SELECT INTO creates that #temp table.
I have a rather complex SELECT statement in a stored procedure that I am updating to insert the rows from the select into a temp table. To define the temp table, I need to know the data type of each every item selected.
Is there a easy way (a script maybe) that I can use to determine the data types and the temp table structure instead of going to each table's definition in the select to find out what it is?
PS: I can't use a Common table expression as I need to use this temp table several times within the proc
SELECT
blah
INTO
#temp
FROM
wibble
blah and wibble are not secret syntax. Please replace these with your own SQL :)
SELECT * INTO #temp FROM TABLE1
All columns in TABLE 1 gets into your temp table now