Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
mkjit256
Advocate II
Advocate II

Concatenate using || failing in pyspark spark.sql(sql_string)

Hello 

I am trying to execute the following code in Fabric notebook (Pyspark cell):

sql_string = """
INSERT INTO dummy_Table (Schema_Name, Table_Name, SQL_Statement)
('schema1', 'table1', "SELECT col1, col2, col3,col4,col5,col6, col5 || ''_'' || col6 as con_col FROM schema1.table1"))

spark.sql(sql_string)

 but i am getting an error around con_col. I tried the following but non of them is working single quote, two single quote, three single quote, 1 double quote but non of them is working,
any suggestions or workarounds are highly appreciated.
Thanks

 

2 ACCEPTED SOLUTIONS
SachinNandanwar
Super User
Super User

Try either of this 

sql_string = """
INSERT INTO dummy_Table (Schema_Name, Table_Name, SQL_Statement)
VALUES ('schema1', 'table1', "SELECT col1, col2, col3, col4, col5, col6, col5 || '_' || col6 as con_col FROM schema1.table1")"""

spark.sql(sql_string)

 

sql_string = """
INSERT INTO dummy_Table (Schema_Name, Table_Name, SQL_Statement)
VALUES ('schema1', 'table1', 'SELECT col1, col2, col3, col4, col5, col6, col5 || ''_'' || col6 as con_col FROM schema1.table1')"""

spark.sql(sql_string)


Regards,
Sachin
Check out my Blog

View solution in original post

Anonymous
Not applicable

Hi @mkjit256 ,

 

Thanks for the reply from SachinNandanwar .

 

Please try this code, it works fine for me:

vhuijieymsft_0-1726536626129.png

 

Replace the schema name, table name, and column name with your own.

sql_string = “””
INSERT INTO dummy_Table (Schema_Name, Table_Name, SQL_Statement)
VALUES ('dbo', 'products', “SELECT ProductID, ProductName, Category, ListPrice, Date, Month, Date || ‘_’) | | Month as con_col FROM dbo.products")
“"”

spark.sql(sql_string)

 

The values keyword is used instead of inserting values directly.

 

If you have any other questions please feel free to contact me.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @mkjit256 ,

 

Thanks for the reply from SachinNandanwar .

 

Please try this code, it works fine for me:

vhuijieymsft_0-1726536626129.png

 

Replace the schema name, table name, and column name with your own.

sql_string = “””
INSERT INTO dummy_Table (Schema_Name, Table_Name, SQL_Statement)
VALUES ('dbo', 'products', “SELECT ProductID, ProductName, Category, ListPrice, Date, Month, Date || ‘_’) | | Month as con_col FROM dbo.products")
“"”

spark.sql(sql_string)

 

The values keyword is used instead of inserting values directly.

 

If you have any other questions please feel free to contact me.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

SachinNandanwar
Super User
Super User

Try either of this 

sql_string = """
INSERT INTO dummy_Table (Schema_Name, Table_Name, SQL_Statement)
VALUES ('schema1', 'table1', "SELECT col1, col2, col3, col4, col5, col6, col5 || '_' || col6 as con_col FROM schema1.table1")"""

spark.sql(sql_string)

 

sql_string = """
INSERT INTO dummy_Table (Schema_Name, Table_Name, SQL_Statement)
VALUES ('schema1', 'table1', 'SELECT col1, col2, col3, col4, col5, col6, col5 || ''_'' || col6 as con_col FROM schema1.table1')"""

spark.sql(sql_string)


Regards,
Sachin
Check out my Blog

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.