I want to check the current connection to validate if the configuration is correct according to the environment. I mean, I have a ETL and I want to be sure the database configuration points to the right server (dev, QA or prod)
How can I get the IP address of the server or hostname from SQL in Teradata?
As a Teradata system might have multiple nodes there's a range of IP addresses assigned to a system. And the hostname is used in your connection string, thus it should be known in advance.
There's no builtin way to get that info from a system table, but it's easy to create a SQL-UDF:
FUNCTION syslib.#servername ()
RETURNS VARCHAR(30)
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
RETURNS NULL ON NULL INPUT
SQL SECURITY DEFINER
COLLATION INVOKER
INLINE TYPE 1
RETURN 'dev';
GRANT EXECUTE FUNCTION ON syslib.#servername TO PUBLIC;
As it's stored in syslib it can be used without qualified name: select servername()
Related
In my database, i have one procedure which has been accessed by two other users through FID (outside of the database).
i need to log the FID of the user who are all using my procedure.
Is there any SQL Query to get the FID in oracle??
You didn't specify how exactly your users are identified (there is no "FID thing" in Oracle RDBMS).
Oracle knows only about two layers of identification:
Session user (for example scott#). This is how to get connected user:
select sys_context('userenv','session_user') from dual;
Proxy user (for example proxy[scott]#). This is how to get connected proxy user:
select sys_context('userenv','proxy_user') from dual;
If your user identification is happening outside Oracle, then you can't catch it in PLSQL. You should add another IN parameter to PLSQL procedure and pass it by your outside app when calling stored procedure.
The standard in our company is to log the user name that inserts or updates a record at the time the record is inserted or updated. We inset and update the tables calling stored procedures from applications. We use SUSER_SNAME() in the stored procedures to get the name. Now we've added inserting records in a table on a linked server. The SUSER_SNAME function not is returning the same login name as it does on the native database. Instead it is returning the name of the account created to link with. I know I can pass the user name as another parameter to the query, but it would be easier if there is a built-in function that works both when connected to the server directly, and when connecting to a linked server. Is there a built-in function that does this?
Stored procedure
INSERT INTO Account (ForeignKey, AccountNumber, IsProcessed,
Origin, Updated, LoginName, Total)
VALUES (#ForeignKey, #AccountNumber, #IsProcessed,
GETDATE(), NULL, SUSER_SNAME(), #Total)
RETURN SCOPE_IDENTITY()
No, there's no function to do this. When you create a linked server, the linked server "opens a connection" to the 2nd server. You, the user, have a connection to the 1st server. So when you run suser_sname() on the 1st server, you get your username that you are connected with. When you run it on the 2nd server, you STILL get the user name that you are using to connect to the second server. The difference is that the connection to the 2nd server is controlled by the administrator that configured the Linked Server. In your case, it sounds like he created a special account that is used for a linked server connection. Another option is to map accounts from the current server to accounts on the 2nd server. (If your DBA had done that, you might be getting the results that you want right now and not had any issues.) There are different ways to configure a linked server, and security should generally be a priority when doing so.
So, if you want to execute a query on the 2nd server and use your user name from the 1st server, then you need to pass it to the 2nd server, because it has no way of knowing who you are on a different machine.
I wanna learn remote machine's ip adressess which accessed my sql server. someone at my job(in local network) find my database password and making row updates. how could I find the access person's ip?
In SQL Server you can try this:
CREATE FUNCTION [dbo].[GetCurrentIP] ()
RETURNS varchar(280)
AS
BEGIN
DECLARE #IP_Address varchar(280);
SELECT #IP_Address = client_net_address
FROM sys.dm_exec_connections
WHERE Session_id = ##SPID;
Return #IP_Address;
END
Add a trigger to the table that adds the server variables like host name to a separate table. See trigger on update. Also select host_name.
I know this can be done in code (C#) by using WindowsIdentity, but is there a way to do this within the SP itself?
I tried using the USER function but it's returning dbo instead of the name. Is this because the user is the owner of the database? If that's so, how can I get the db owner's login name?
EDIT:
The application that executes the SP uses the sa user account, so if I use SYSTEM_USER, the returned value is sa.
SELECT SYSTEM_USER
Will return name of user which excecute code in SQL. More in this aricle
If SQL server service work from sa there is no way to get (windows) username from SQL Server side. I suppose it is security issue. Most information about connection to your SQL Server you may get from table sys.dm_exec_connections. There are stored IP addreses and port s and other useful things for existings connections.
I'm making a call to odbc32.dll (SQLBrowseConnect) to return a list of databases on a sql server.
From running a trace I can see the query being executed is
select name from master..sysdatabases where has_dbaccess(name)=1
If the credentials I pass aren't the sa user it returns just the system databases. Is there anyway I can use SQLBrowseConnect with another user (whose default database is also not guarenteed to be the master database) to return all databases on the server?
Also I want to avoid smo objects
The query does work without sysadmin credentials.
You need to ensure that you / the credentials you are using are at least in the public database role on each of the databases that you need to connect to.
As you would guess, select name from master..sysdatabases returns all database names irrespective of your access to the DB.