Requirement is to get program name that runs sql command and store it in a column in audit table. Have added login id using 'suser_name()' function. But need to program that is running the command.
I'm using Sybase Adaptive Server 12.5.
Thanks!
This is what I was looking for:
select
suser_name (suid) as 'suser_name',
ipaddr,
program_name,
loggedindatetime
from master..sysprocesses
where spid = ##spid
Related
I need to copy data from a linked server Oracle table and append to SQL Server table in a regular time interval.
Without using SSIS or any external etl, can this be done by using open query? I have read in forums that the table has to be exported to csv and then imported back again into SQL Server . Is there an alternative as this will be continuous process ?
Certainly. As long as you've configured the Linked Server properly within SQL Server, and your user account has the right permissions in Oracle, getting and sending data between the two is fairly trivial.
Selecting data from Oracle into SQL Server:
IF OBJECT_ID('tempdb..#myTemp') IS NOT NULL DROP TABLE #myTemp
SELECT *
INTO #myTemp
FROM OPENQUERY(MyLinkedServer, 'SELECT col1, col2
FROM OracleTableName
WHERE SomeColumn = ''Human Resources''')
Inserting data from SQL Server into Oracle:
-- note: please make sure you have your columns in the exact same order!
INSERT INTO OPENQUERY(MyLinkedServer, 'SELECT col1
FROM OracleTableName ')
SELECT myPK
FROM AdventureWorks2014.dbo.SomeTable
And you can even drop or recreate a table in Oracle from SQL Server, and run stored procedures.
To create a linked server, I use the below script and these settings:
-- make sure you have Oracle drivers on your SQL Server, and an up-to-date TNSNames.ora file
EXEC master.dbo.sp_addlinkedserver #server = N'MyLinkedServer', #srvproduct=N'Oracle', #provider=N'OraOLEDB.Oracle', #datasrc=N'TNS_NAME_HERE'
-- disable any logins not explicitly mapped
EXEC master.dbo.sp_droplinkedsrvlogin #rmtsrvname = N'MyLinkedServer', #locallogin = NULL
-- login mapping between SQL Server user and their appropriate Oracle account
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'MyLinkedServer',#useself=N'False',#locallogin=N'domain\username',#rmtuser=N'ORACLE_USERNAME',#rmtpassword='ORACLE_PASSWORD'
-- add a mapping for your SQL Agent account if these will run under jobs,
-- and your SQL Engine account if you still get errors on things that are automated,
-- unless those automated jobs are explicitly running under user accounts
Honestly, the hardest part is making sure you get Oracle drivers installed right on the SQL Server. After that, it's very straightforward.
I wanted to connect to 2 database at the same time in pro c code on Linux, and run SQL queries simultaneously on both the databases in single running application. Is this possible. Please suggest.
We can use below syntax for this
EXEC SQL CONNECT :uName1 IDENTIFIED BY :passwd1 AT :DB_NAME1 USING :dbString1
EXEC SQL CONNECT :uName2 IDENTIFIED BY :passwd2 AT :DB_NAME2 USING :dbString2
EXEC SQL at :DB_NAME1 SELECT .....
EXEC SQL at :DB_NAME2 SELECT .....
I recently installed and have been using Microsoft SQL Server Management Studio Express 9.00.4035.00 to manage a remote database on my hosts' SQL Server 2005.
Until now I had been using EMS SQL Manager 2011 Lite and that was working fine.
2 new rows where inserted into one of my tables this morning; one by a customer signing up for a service and the other as a test signup by me.
When I run a typical select query:
[Select top 20 * From tblNotary Order By ID Desc]
I don't see the the 2 most recent rows. But when I run the same query from EMS SQL Manager Lite I see the records.
I also verified when connecting using MS Access 2010 I see the 2 new rows in the table. I have checked and double-checked the connection settings and they match EMS.
Is there a setting or something obvious I am missing? Why can't I see the most recent record insertions? I am on a Windows 7 desktop machine.
The most likely reason is you are connecting to a different database than you expected. You can select ##servername to verify both queries are running against the same server.
If the records are stil being inserted as part of an open transaction and have not been committed, they are called "phantom" rows. You will not see phantom rows if your query runs at transaction isolation level read committed or higher. It may be that EMS SQL Manager Lite is running at read uncommitted, in which case it will include phantom rows in the query.
Sometimes connection strings are a tricky thing. Run this to double check:
select ##servername servername, name, crdate
from sys.sysdatabases
where name = db_name()
if you have an issue running that, just do a simpler version:
select ##servername, db_name(), ##version
Select top 20 * From tblNotary Order By ID Desc
Following is the error message I get when I tried to execute Select * for the table in SQl Server, Any suggestions would be appreciated.
"lock request time out period exceeded
sql server"
Look at SQL hints, in particular WITH (NOLOCK)
http://www.developerfusion.com/article/1688/sql-server-locks/4/
When you do you SELECT you're locking the table. But your SELECT takes so much time, that SQL ends it, in order not to keep the table locked, mainly because SQL server thinks it's a bug.
Once upon a time, I remember running some TSQL which would display which network protocol (e.g. named pipes, TCP/IP) was being used by each active session - but I can't remember the actual code. Does anyone know how to do this ? An input parameter might have been SPID. The code was useful for diagnostic purposes.
For SQL Server 2005+ sys.dm_exec_connections has "net_transport" column
sysprocesses has a net_library column valid for older SQL Server too
So, a SELECT net_library FROM sys.sysprocesses WHERE SPID = 123 /* use ##SPID for current connection */ will give you this info.