My application is multilingual application. In my application, I have created a report which has 2 data source. Both call same procedures and fetch same data. The difference only is based on culture, the data will be filled and table will be displayed.
It is working fine. But the problem I am facing is, though I have applied condition to display table based on culture, it is executing both data source. That is I have one unnecessary database call.
Is there any alternative to solve this?
If your data source comes from SQL, I believe you can do this in a stored procedure with a report parameter. (I assume your tables are in the same database, but you can adjust accordingly)
I will use the AdventureWorks database, and my sp selects the "Name" column from a given table:
CREATE PROCEDURE [dbo].[test] #table nvarchar(100) as
BEGIN
declare #sql nvarchar(1000)
SET #sql = 'SELECT Name FROM ' + #table
exec SP_EXECUTESQL #sql
END;
Then, configure the datasource to execute the stored procedure, taking in a report parameter for #table. In my example, reportParameter="Person.AddressType" or "Person.ContactType" will give different results.
In your case, change the reportParameter according to culture. Now you will only need one table instead of two as well.
Related
I'm creating an application that reads data from a PLC and writes it into a database.
In my GUI the user can select which data he wants to log into the database, but I'm wondering how to solve this.
sometimes the user wants to log only one item in the database :
INSERT INTO myTabel (name_data1) VALUES ("123")
but other times I must be able to choose more data :
INSERT INTO myTabel (name_data1, name_data2, name_data3) VALUES ("123", "456", "789")
how would I code this? The column names name_data1, name_data2, name_data3, ... in my database should be able to change according to which data is chosen
for this purpose I use a list of 2 strings
["name_data1", "123"],
["name_data2", "456"],
["name_data3", "789"]
so the problem also is that I do not know how the table will look like at the moment I insert the data, it all depends on which data the user chooses to be inserted into the database
If I'm correct this is called creating dynamic columns, but I do not seem to find much info on this subject.
yo can always use all the columns and if a value is not provided you pass null
like
INSERT INTO myTabel (name_data1, name_data2, name_data3) VALUES ("123", null, "789")
but if you want to use dynamic sql which is not always recommended would look like this :
DECLARE #sqlstatement as varchar(1000)= 'insert into myTabel (name_data1, name_data2, name_data3) VALUES ( #col1, #col2, #col3)'
declare #parmlist = '#col1 varchar(50) , #col2 varchar(50), #col3 varchar(50)'
exec sp_executesql
#sqlstatement
,#parmlist
, #col1 = '123'
, #col2 = NULL
, #col3 =NULL
I have this stored procedure to retrieve data from a database (dynamic query). I am calling this stored procedure from C# code, passing two parameters to this stored procedure:
ALTER PROCEDURE [dbo].[GetCompleteCPTDetails]
#Practice_Short_Name varchar(50),
#Uploaded_Date nvarchar(max)
AS
BEGIN
DECLARE #CPTtablename nvarchar(300)
DECLARE #vQuery NVARCHAR(max)
DECLARE #upldate nvarchar(100)
SET #upldate = #Uploaded_Date
SET #CPTtablename = 'ACER_CLAIMS_MASTER_DETAIL_Hist_' + #Practice_Short_Name
SET #vQuery = 'SELECT Practice_Short_Name, Service_Date_From, Carrier_Name,
Location_Description, Patient_Number, Patient_First_Name,
Patient_Last_Name, Voucher_Number, Procedure_Code, Service_Fees,
Service_Payments, Service_Adjustments, Acer_Status, Acer_Allowed_Amount
FROM ' +#CPTtablename+'
WHERE Uploaded_Date =''' + #upldate + '''
ORDER BY acer_status ASC, Service_Date_From DESC, Patient_First_Name ASC'
EXEC (#vQuery)
END
But when I am running this query I get a timeout error. If I assign value to my parameters in the stored procedure and run it from query windows then it is showing correct data.
Can anyone please explain to me why I get a timeout error if I am calling it from C#?
That is a pretty simple where and order by.
Unless that is just a massive table with no indexes that should be fast.
Is there an index on Uploaded_Date and is it not fragmented.
Also an index on the sort would help.
Are you loading everything into a DataTable?
If so try loading into DataReader.
Try a top 1 and remove the order by.
If that does not return then you have connection issue as no way that query should time out.
The other thing to try is with (no lock) to see if it is a lock problem.
Why is #Uploaded_Date nvarchar(max)?
Is that a date or not?
There can be many solutions to this problem, as problem areas can be different in each case.
But most common:
Check & increase sqlcommand timeout in your application
Try calling this SP asynchronously
Also i would like to know, your application on the same machine where DB resides?
I have written a stored procedure to insert values into a table where the primary key will be auto incremented. when I try to import this in Visual Studio 2013, In function Imports when I select "Get Column Information" it says "The selected procedure or function" returns no columns.
I read about it many articles and also included SET FMTONLY OFF in my code but it still does not work. Amateur in ASP.Net and C#. So can anyone explain to me What to do in a clear manner
USE [DB_Name]
GO
/****** Object: StoredProcedure [dbo].[usp_makePost] Script Date: 04-04-2015 19:16:04 ******/
SET FMTONLY OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[usp_makePost]
#FK_strUser_Id varchar(11),
#strPost_Title varchar(100),
#strPost_Content varchar(1000),
#dTime_of_post datetime,
#iUp_Vote int,
#iDown_Vote int,
#FK_strRootword_Id varchar(11)
as
begin
DECLARE #PK_strPost_Id VARCHAR(11);
DECLARE #PreFix VARCHAR(10) = 'POST';
DECLARE #Id INT;
SELECT #Id = ISNULL(COUNT(PK_strPost_Id),0) + 1 FROM Tbl_Post
SELECT #PK_strPost_Id = #PreFix + RIGHT('0000' + CAST(#Id AS VARCHAR(7)), 7)
insert into Tbl_Name values(#PK_strPost_Id,#FK_strUser_Id,#strPost_Title,#strPost_Content,#dTime_of_post,#iUp_Vote,#iDown_Vote,#FK_strRootword_Id)
end
Your stored procedure doesn't do any data retrieve operation (ie, any SELECT). It just does an INSERT plus some variable manipulation. Those SELECTs out there only assign variables too, so nothing really produces any kind of result set.
Therefore client programs are completely right in that there are no columns or any kind of output from this procedure. Maybe you intended to add some sort of return table?
Think of the stored procedure as a data source for your front end application. Now for it to have data, it has to end with a SELECT clause, because only then can it have data. Clearly your application is expecting data. So without going into much details,
either you need to tell your application to stop expecting data.
or, modify the procedure so that it starts giving data.
Probably you would need to add a SELECT * FROM Tbl_Name in the end of stored proc or something similar.
I have looked for a few days for a solution but can't find one.
I am creating a stored procedure to search a table using fulltext search. I will then combine the result from 15 of these stored procedures into a list ordered by their ranking.
Here is the code for the stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SpAdvancedSearchICAPUsers]
#searching nvarchar(100) = ''
AS
BEGIN
SET NOCOUNT ON;
Set FMTONLY OFF;
DECLARE #SQLQuery AS NVarchar(max)
IF #searching IS NULL OR #searching = ''
BEGIN
SET #SQLQuery = 'SELECT (all field names listed separated by commas) FROM Users'
END
ELSE
BEGIN
SET #SQLQuery = 'SELECT (all field names listed separated by commas), fts.Rank FROM Users as p JOIN
FREETEXTTABLE(Users, (fields to search for listed separated by commas), #searching)
as fts on p.userID = fts.[KEY] WHERE 1=1'
END
EXEC #SQLQuery
END
I did approached this project doing Model first. I added my stored procedures to my model by right clicking and pressing: Add New > Function Import...
I set the name of the function import, selected the stored procedure, selected the "Returns a Collection Of" to Entities: (desired data type the SP returns, in this case, Users).
When I use the stored procedure like this:
newSearch.Users = db.SpAdvancedSearchICAPUsers(search); //search is a string
newSearch.Users is an IENumerable<Users>. It says the return type is an int for the stored procedure. I get the following error:
Cannot implicitly convert type 'int' to 'System.Collections.Generic.IEnumerable;
I have also tried adding a parameter definition variable as such right below declaring #SQLQuery like this:
Declare #ParamDefinition As NVarchar(4000)
and then I set it and try and run it like this:
Set #ParamDefinition = '#searchingnvarchar(100)'
Exec sp_executesql #SQLQuery, #ParamDefinition, #searching
#searching is the string which is passed in to search.
I have even tried dragging all of my tables into a DBML file, because I've used stored procedures this way successfully before. When I dragged in the SP to the table I get this message:
UPDATE: Eddie in the comments specified that in the DBML after your drag in the stored procedure, you can set the return type (I chose the User object type). This works. I would rather not do this though. I'll have it as a temporary solution but I have 15 stored procedures I'd have to do that to.
Does anyone know how to get the correct output from the stored procedure using the EDMX file instead of a DBML? When I look at the properties after right clicking in the EDMX and selection "Mapping Details > Stored Procedures / Functions". The return type in the EDMX doesn't have anything. It has no drop downlist, nothing.
I'm not sure if I'm hitting the spot here, but if you are doing something like newSearch.Users = db.SpAdvancedSearchICAPUsers(search);
and newSearch.Users is of type User Entity, then perhaps writing the search statement as
Set #SQLQuery = 'SELECT p.* FROM Users as p JOIN
FREETEXTTABLE(Users, (fields to search for listed separated by commas), #searching)
as fts on p.userID = fts.[KEY] WHERE 1=1
ORDER BY fts.[Rank]'
would work for the simple fact that it is returning the fields a User Entity expects (i.e.: no rank field).
To better diagnose whether this is the case or not, the question turns to:
Does the same problem happen when you send an empty search field?
i.e.:
newSearch.Users = db.SpAdvancedSearchICAPUsers('');
If it works for the latter case, then perhaps the aforementioned change would do.
Please let me know.
I have a System that fetches its data from different DBs on the same server.This DBs are newly attached to the server annually.e.g. at the at the beginning of 2013, a db called 2012 is attached.
So I want to create a stored procedure(SP) that fetches the user's input which can be anything from 2005(year). so based on the year the user enters, the SP should go to that db(whose name will be the year the user entered) and search for the data (with its parameter being the year the user entered) inside the db which will also has a table with the same name as the db(i.e the table will have the same name as the year name).
Hope this makes sense
It would be a good idea to paramatarize the query.
e.g.
CREATE PROC usp_bar
(
#ID INT
)
AS
BEGIN
DECLARE #SQL NVARCHAR(100)
DECLARE #Params NVARCHAR(100)
SET #SQL = N'SELECT * FROM [Table] WHERE ID = #ID'
SET #Params = N'#ID INT'
EXEC sp_executesql #SQL, #Params, #ID = 5
END
Check out this
I am no DBA so take this with a grain of salt and understand there may be a better way to do this but what you will probably have to do is something like this:
CREATE PROCEDURE usp_foo
#Year varchar(4)
AS
DECLARE #sql varchar(255)
#sql = 'SELECT * from [' + #Year + '].[owner].[table]'
sp_executesql #sql
Of course the user calling this sproc will have to A) have permissions to call system sprocs B) have permission to access the yearly database
Additionally, instead of going the SQL route, you could just make a dynamic connection string that you could populate with the correct catalog then issue your SQL queries directly to the database. Personally I would prefer that over using dynamic SQL.
One thing you could do, is take a look at synonyms, you could create one for each year:
http://sommarskog.se/dynamic_sql.html#Dyn_DB
CREATE SYNONYM otherdbtbl FOR otherdb.dbo.tbl
I'd recommend his site, it's full of good stuff, it's a great read :)
As to whether this works well for you, I'd expect it depends on how many tables you have in each DB. If it's a few this may work, if it's hundreds maybe another approach may work better - like abszero's suggestion of doing the switching in the application tier?