I need to create a stored procedure in SQL Server using the Dbeaver software and I am wondering if you have any experience with creating one with this software since I am using Mac and I am not able to install SQL Server Management Studio.
The case is that I will need to update the field for one user across all the courses assigned to that user (about 20).
It will be something like this :
UPDATE tablename
SET date = '2018-03-15'
WHERE UserID = 1234 AND ProgramID = 1234;
It is updating the date fine.
From this point, my question is about would it be better to use UPDATE or something else since they just need to view the report for this data inside of UI? How would you start the script for this procedure and what would you use? Thanks
From your conclusive question I suppose you want the syntax to create a procedure which you can learn from so many sites available with great content. To create procedure for this specific activity your procedure will look something like this. You can dynamically add date, user's Id and program's Id, if you want to take the report for some other date depending on your needs. Hope this helps.
CREATE OR REPLACE PROCEDURE updatedate(#dateupdate datetime, #ProgramID INT, #UserID INT)
AS
BEGIN
UPDATE tablename
SET date = #dateupdate
WHERE UserID = #UserID AND ProgramID = #ProgramID;
END;
To execute your procedure just replace the variables with the values you want in below query.
updatedate(#dateupdate, #ProgramID, #UserID)
This is an example for MySQL but should work with any database:
Right Click on a database and choose Create -> Procedure
Fill in the details of what you want for the procedure.
Open up the procedure editor and go to Source tab and paste your source
Save
Persist
Related
I have a report with a lot of datasets, and I want to make it easy for maintenance. I created a stored procedure that runs multiple select statements and accesses the data with parameters.
Sample stored procedure:
CREATE [dbo].[sp_test_report]
#inputReportPart varchar(20)
AS
BEGIN
IF (#inputReportPart = '1' OR #inputReportPart = 'debug')
BEGIN
SELECT a, b, c
FROM table1
END
IF (#inputReportPart = '2' OR #inputReportPart = 'debug')
BEGIN
SELECT q, w, e
FROM table2
END
END
But when I implement it to SSRS, the default column always use the first query even the parameter value is different.
Result:
Dataset view
Dataset1 property
Dataset2 property
I try to rebuild solution, refresh field, and delete .rdl.data but it doesn't work
Any solution other than add the column manually into dataset?
i want to make it easy for maintenance then i created stored procedure that store multiple select
That won't make anything easier. Use a different stored procedure for each dataset.
SSRS doesn't actually run your procedure to determine the dataset schema. So it will always discover the first resultset in the procedure, whether it uses FMTONLY or the newer sp_describe_first_result_set.
If you really wanted to make this work you'd have to add WITH RESULT SETS to your Dataset query which enables the client to specify the resultset metadata.
I am new to SQL Server. I wrote a simple stored procedure that returns rows with data condition.
Here is my code:
CREATE PROCEDURE test.newArtists
#LastUpdated smalldatetime
AS
BEGIN
SELECT *
FROM ARTIST
WHERE GENERATED > #LastUpdated OR MODIFIED > #LastUpdated;
END
When I execute this stored procedure, it returns 0 rows. like ...
DECLARE #temp DATETIME;
SET #temp = CONVERT (DATETIME, '2016/12/05');
EXEC test.newArtists #LastUpdated = #temp;
However, when I execute the query without using procedure, it returns about 5,000 rows.
DECLARE #temp DATETIME;
SET #temp = CONVERT (DATETIME, '2016/12/05');
SELECT *
FROM ARTIST
WHERE GENERATED > #temp OR MODIFIED > #temp;
I just do not understand whey those two returns different results.
Thanks for explanations!
===================================================
I find the problem. Thanks.
I'am using test schema.
So I connected to test. However, When I use SELECT * FROM ARTIST it does not search test.ARTIST, but ARTIST table which belongs to dbo.
Summary.
The basic problem here is I got 2 tables with name ARTIST.
However, I still do not understand why it automatically look for dbo schema.
I got little experience with MYSQL, but when I connect to a certain schema, it only find objects inside of the schema. Is it normal or should I do some work to set prefix?
Thanks for answers though b
It is always good practice to refer to database objects by a schema
name and the object name, separated by a period (.).
object referred to without an explicit schema name ... will be located by searching the default schema first, followed by the dbo schema
Source: SQL Server Best Practices – Implementation of Database Object Schemas
Dbo is the default schema, it will always go to that one first if the schema is not specified. It is a good practice to always specify the schema in the query especially if you are using more than one. That way it won't waste time looking in the wrong schema first.
I have procedure dbo.GetData:
Create procedure dbo.GetData
As
Begin
Select * from dbo.tblName
End
And I also created a schema [ABC], table ABC.tblName
So, I would like to change schema [dbo] of table in procedure dbo.GetData into [ABC] by using another stored procedure.
And, the result is:
Create procedure dbo.GetData
As
Begin
Select * from [ABC].tblName
End
How can I do it?
Thank you everyone.
I'm not sure I understand what you're asking, but I think you simply want to change the code being executed in the stored procedure. If so, a simple ALTER PROCEDURE would do the trick to change the code, but not the name:
ALTER PROCEDURE dbo.GetData
AS
BEGIN
SELECT * FROM [ABC].tblName
END
Full syntax of [ALTER PROCEDURE] 1 (for SQL Server)
If this is not what you're after, please clarify the question.
Update:
The only real solution I see is that you script out your procs, and then use a text-editor to replace the dbo. values with [ABC]. values.
I just attempted to try and do this through updating the system tables, but in SQL Server 2012 (which I use), it simply gets far too complex for that.
Try this hope this may help you!
ALTER SCHEMA NewSchemaName TRANSFER OldSchemaName.ObjectName
suppose i am calling a Stored Procedure and sending some params. the Stored Procedure will update a table and a trigger is there for that table. so when Stored Procedure update table then i want which user is updating the table but i am not using sql server user name rather i will send user names to SP who will update the table. so before update i can store that user name in local temp table and query that local temp table from trigger to get that user name. i hope it is possible but i want to other good option to achieve this job. so please share idea. thanks
You can encode the user name in the procedure into the session using SET CONTEXT_INFO
This can be read in the trigger by CONTEXT_INFO
This is concurrency safe: each session/call is isolated from any other.
And no need for a temp table or such.
DECLARE #context varbinary(100) = CAST('foobar' AS varbinary(100));
SET CONTEXT_INFO #context;
SELECT CAST(CONTEXT_INFO() AS varchar(100));
I'm working on the next update for StackQL.
One thing I want to do is have the ability to query over several releases. So when I loaded the October data, for example, I didn't delete the old September database. It's still out there. In fact, you can even still query it by including the database name like this:
select top 10 * from SO_Sept09..Posts
This will be even more important as they start providing data for ServerFault and SuperUser.
But I don't like having a whole bunch of databases out there to support this. I'd much rather put all the data in the same database and separate each distinct set into it's own schema. But to make this possible, I need to be able to set a default schema as part of the stored procedure that runs the query, based on a parameter passed to the stored procedure that tells it which database the user selected from a future drop down list to appear in the tool bar.
Queries at StackQL are eventually just passed to the exec() function like this:
exec(#QueryText)
Is there anything I can do either in the stored procedure or prepend to the QueryText string (ala USE [DatabaseName]) to set the default schema used in a query?
There are pieces of how to do this in various places here, but not altogether. The way to do this is:
Make a unique login & user for each schema
Make these users the owners of each different schema.
Set each such user's default schema to be the schema that they own.
Use the syntax EXECUTE ('sql commands') AS USER = 'schema-owner' to execute your SQL commands in the context of that default schema.
The following script demonstrates this:
--====== Create the Login for the User:
CREATE LOGIN [UserTest1] WITH PASSWORD='whatever', DEFAULT_DATABASE=[TestUsers], DEFAULT_LANGUAGE=[us_english]
GO
--====== Make a User for the Login:
CREATE USER [UserTest1] FOR LOGIN [UserTest1]
GO
--====== Make a Schema owned by the User and default to it:
-- (I assume that you already have the schemas)
CREATE SCHEMA [UserTest1] AUTHORIZATION [UserTest1]
GO
ALTER USER [UserTest1] WITH DEFAULT_SCHEMA=[UserTest1]
GO
--====== Make a sProc in dbo
CREATE PROCEDURE [dbo].[TestSchema_Exec] AS
SELECT 'executing in schema [dbo]'
GO
--====== Make a similar sProc in New Schema
CREATE PROCEDURE [UserTest1].[TestSchema_Exec] AS
SELECT 'executing in schema [UserTest1]'
GO
--========= Demonstrate that we can switch Default Schemas:
EXEC('TestSchema_Exec')
EXEC('TestSchema_Exec') AS USER = 'UserTest1'
Other than modifying #QueryText itself, the only thing I can think of is a user's default schema:
ALTER USER SO_Sept09_Reader WITH DEFAULT_SCHEMA = SO_Sept09
...and then connect as a different user for each schema you want to use. Hackity hack.
But if your query is dynamically constructed anyway (and I'm sure you know why that's often not a great idea), it might be easiest to just add a schema placeholder to the query text, and pass the schema name along with the query to a replacement function.
Another possibility is to generated copies of each SP in each schema, the unmodified table name in an SP refers to tables in the same schema.
Note this doesn't work with dynamic SQL inside such an SP:
CREATE PROCEDURE schema_a.SP
#somesql AS varchar(MAX)
AS
BEGIN
EXEC ( #somesql )
END
CREATE PROCEDURE schema_b.SP
#somesql AS varchar(MAX)
AS
BEGIN
EXEC ( #somesql )
END
Won't work, because the schema affinity is lost inside the EXEC.
While this:
CREATE PROCEDURE schema_a.SP
AS
BEGIN
SELECT * FROM tbl -- Will use schema_a.tbl first
END
CREATE PROCEDURE schema_b.SP
AS
BEGIN
SELECT * FROM tbl -- Will use schema_b.tbl first
END
works fine.
Similarly:
EXEC ( 'EXEC schema_a.SP' )
would obviously work fine.
Untried, but: could you combine the different schemas' data into one view, and add a column calling out the schema name?
CREATE VIEW AllPosts AS
SELECT Data1, Data2, 'Sept09' AS Partition FROM SO_Sept09..Posts
UNION ALL
SELECT Data1, Data2, 'Oct09' AS Partition FROM SO_Oct09..Posts
...
SELECT * FROM AllPosts WHERE Partition = 'Sept09'
SELECT * FROM dbo.AllPosts('Sept09') -- if use table-valued function instead
Okay, I have a new way to do this that might work a little better for me. It's a variant on my comment to Michael Petrotta's answer:
But it does give me the idea to maybe have several users and pick the connection string on the fly.
What I will do is have just one user for executing these queries, but I will swap out the connection string on the fly to specify the correct Initial Catalog.