Change login name when name is with domain and dot - sql-server

I have a problem with renaming user name to new username (I would like it to be shorter).
Original syntax should be like this:
ALTER LOGIN Mary5 WITH NAME = John2;
But my user has name like this: domain/name.lastname and sql server is giving me error, trying to execute this:
ALTER LOGIN mydomain\fname.lstname WITH NAME = shortername
Error:
Incorrect syntax near '\'.
I've tried to put both names in single quotes, which drops another syntax error. How to rename that user?

Normally you would specify the user with brackets like below. But apparently you cannot remove the domain from a user once it's been created with one. You must delete and recreate the user.
ALTER LOGIN [mydomain\fname.lstname] WITH NAME = [shortername]
When specifying a different domain user you will receive the following error: "The name change cannot be performed because the SID of the new name does not match the old SID of the principal.". Further suggesting you cannot simply alter a login based on a domain user.

Related

How to insert username in VS2008(report edition)

I'm creating a new report (*.rdl), and there I want to add username who runs the script (insert).
I've tried on VS2008 through "built-in-fields" function which is "User ID", but it didn't work:
CREATE TABLE #Some_Table
(
Plan_date date null,
Plan_customer int null,
creator_id nvarchar(55) null
)
INSERT INTO Some_Table
(
[Plan_date] ,
[Plan_customer],
[creator_id]
)
SELECT
#p_plan_monthly,
#p_plan_clients,
#creator_id ="user id" --from built-in-fields
Expected result is: Column creator_id is filling with value of username from active directory who made insert through my report.
To reiterate my comment, as it's is incredibly important:
"You need to use a different account to access your data #whitefang. The sa account should never be used for something as mundane as a report. In truth it should never be used unless you really need sysadmin privileges, or you're doing something like recovering the server. You should have a service account that can do the respective tasks it needs to. If you can suffer injection through those reports, you're service is like an open book to whomever has access."
Now, onto your problem. I would add a further internal parameter on your report. Change the value of the parameter to have the default value of =User!UserID; this will be the ID of the user running the report (perhaps something like StackOverflow\Larnu).
Then map that report parameter to your dataset parameter #creator_id and change your INSERT statement to:
INSERT INTO Some_Table ([Plan_date],
[Plan_customer],
[creator_id])
VALUES (#p_plan_monthly, #p_plan_clients, #creator_id);
Q: "and there I want to add username who runs the script (insert)"
You can use these functions.
-- database user name
SELECT USER_NAME()
-- login identification name
SELECT SUSER_NAME()

obtain the real identity of the connected user

dxStatusbar1.Panels1.Text :=
DataModule2.UniConnectDialog1.Connection.Username;
...gives me the username that has connected to sql server.
However the connected user has a different name in the actual database.
Example:
His login name for the sql server is 'John' and is user mapped to 'Northwind' database.
However in 'Northwind' database he is called 'John Smith'.
And this is the name (John Smith) I am trying to have displayed in dxStatusbar1.Panels1.Text
after he connects.
How can I get that ?
edit :
Tried Victoria suggestion :
UserName := DataModule2.UniConnection1.ExecSQL('SELECT :Result = CURRENT_USER', ['Result']);
dxStatusbar1.Panels[1].Text := UserName;
but get :
I couldn't find any UniDAC API way to get currently connected user name (not even for SDAC), so I would just issue a SQL command querying CURRENT_USER and grab the name from the result:
SELECT CURRENT_USER;
Or in the Unified SQL way with the USER function:
SELECT {fn USER};
Since you've mentioned stored procedure in your comment, it sounds to me like you probably want to get this information directly from a connection object without using query object. If that is so, you don't even need to have a stored procedure but execute directly command like this:
var
UserName: string;
begin
UserName := UniConnection1.ExecSQL('SELECT :Result = CURRENT_USER', ['Result']);
...
end;
Or in unified way:
var
UserName: string;
begin
UserName := UniConnection1.ExecSQL('SELECT :Result = {fn USER}', ['Result']);
...
end;
One of these might do the job for you. Haven't tested.
SELECT ORIGINAL_LOGIN()
SELECT SYSTEM_USER
SELECT SUSER_SNAME()
Hope it helps.
ORIGINAL_LOGIN: Returns the name of the login that connected to the instance of SQL Server. You can use this function to return the identity of the original login in sessions in which there are many explicit or implicit context switches.
SYSTEM_USER: Allows a system-supplied value for the current login to be inserted into a table when no default value is specified.
SUSER_SNAME: Returns the login name associated with a security identification number (SID).

Oracle changing password, where old password contain #-signs

Let say i have a password name T#mp
I want to change it to Hello
but if i use this following query with my user it wont compile as my old password contain #-signs
alter user MYUSER identified by Hello replace T#mp
I am using TOAD
I am changing my password to normal character because, #-signs also creating problem in SQL loader
sqlldr myuser/T#mp#prodcms control=loading.ctl
As #Wernfried Domscheit has shown, you have to enclose the old password in double quotes as shown below.
SQL> alter user sales_hr identified by password replace p#assword;
alter user sales_hr identified by password replace p#assword
*
ERROR at line 1:
ORA-00922: missing or invalid option
SQL> alter user sales_hr identified by password replace "p#ssword";
User altered.

How to make the 'public' schema default in a Scala Play project that uses PostgreSQL?

I am not sure if my issue connecting to the Scala Play 2.5.x Framework or to PostgreSQL so I am going to describe my setup.
I am using the Play 2.5.6 with Scala and PostgreSQL 9.5.4-2 from the BigSQL Sandboxes. I use the Play Framework default evolution package to manage the DB versions.
I created a new database in BigSQL Sandbox's PGSQL and PGSQL created a default schema called public. I use this schema for development.
I would like to create a table with the following script (1.sql in DB evolution config):
# Initialize the database
# --- !Ups
CREATE TABLE user (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
creation_date TIMESTAMP NOT NULL
);
# --- !Downs
DROP TABLE user;
Besides that I would like to read the table with a code like this:
val resultSet = statement.executeQuery("SELECT id, name, email FROM public.user WHERE id=" + id.toString)
I got an error if I would like to execute any of the mentioned code or even if I use the CREATE TABLE... code in pgadmin. The issue is with the user table name. If I prefix it with public (i.e. public.user) everything works fine.
My questions are:
Is it normal to prefix the table name with the schema name every time? It seems to odd to me.
How can I make the public schema a default option so I do not have to qualify the table name? (e.g. CREATE TABLE user (...); will not throw an error)
I tried the following:
I set the search_path for my user: ALTER USER my_user SET search_path to public;
I set the search_path for my database: ALTER database "my_database" SET search_path TO my_schema;
search_path correctly shows this: "$user",public
I got the following errors:
In Play: p.a.d.e.DefaultEvolutionsApi - ERROR: syntax error at or near "user"
In pgadmin:
ERROR: syntax error at or near "user"
LINE 1: CREATE TABLE user (
********** Error **********
ERROR: syntax error at or near "user"
SQL state: 42601
Character: 14
This has nothing to do with the default schema. user is a reserved word.
You need to use double quotes to be able to create such a table:
CREATE TABLE "user" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
creation_date TIMESTAMP NOT NULL
);
But I strongly recommend not doing that. Find a different name that does not require a quoted identifier.

How to check if string a exists in a specified column in database

this is my sample access database:
ID--UserName--Password--AccountType
1---- A123 --1234 --User
2-----B123 --1345 --Admin
I am using VS2012. In my VB.net Project I have username textbox, a password textbox,
and login button.
I add my database using a wizard. I can add, modify, delete, and query, but how to check if the entered username in username text box exists in UserName column?
I filled up my dataset using:
Me.UsersTableAdapter.Fill(Me.WSDataSet.users)
and if I want to get the user type I am using:
Me.WSDataSet.users.FindByUserName(IDtxt.Text).AcountType
but the main problem if user not exists I get the error below:
An unhandled exception of type 'System.NullReferenceException' occurred in user login.exe Additional information: Object reference not set to an instance of an object.
How can I check if the username exists or not?
Try doing this.
Dim user = Me.WSDataSet.users.FindByUserName(IDtxt.Text)
If not user is nothing Then
'Do what you want with the user object
Else
'Message User does not exist.
End If
you just check if the user exists then do what you want with it.

Resources